Coder Social home page Coder Social logo

blag / django-tz-detect Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adamcharnock/django-tz-detect

0.0 3.0 0.0 72 KB

Easy user timezone detection for Django

Home Page: https://pypi.python.org/pypi/django-tz-detect

License: MIT License

Python 80.99% HTML 4.01% JavaScript 15.01%

django-tz-detect's Introduction

django-tz-detect

This app will auto-detect a user's timezone using JavaScript, then configure Django's timezone localization system accordingly. As a result, dates shown to users will be in their local timezones.

Authored by Adam Charnock, and some great contributors.

https://coveralls.io/repos/adamcharnock/django-tz-detect/badge.svg?branch=develop https://landscape.io/github/adamcharnock/django-tz-detect/develop/landscape.svg?style=flat

How it works

On the first page view you should find that tz_detect places a piece of asynchronous JavaScript code into your page using the template tag you inserted. The script will obtain the user's GMT offset using getTimezoneOffset, and post it back to Django. The offset is stored in the user's session and Django's timezone awareness is configured in the middleware.

The JavaScript will not be displayed in future requests.

Installation

  1. Either checkout tz_detect from GitHub, or install using pip:

    pip install django-tz-detect
  2. Add tz_detect to your INSTALLED_APPS:

    INSTALLED_APPS += (
        'tz_detect',
    )
  3. Be sure you have the django.template.context_processors.request processor

    TEMPLATES = [
        {
            ...
            'OPTIONS': {
                'context_processors': [
                    ...
                    'django.template.context_processors.request',
                ],
            },
        },
    ]
  4. Update your urls.py file:

    urlpatterns += [
        url(r'^tz_detect/', include('tz_detect.urls')),
    ]
  5. Add the detection template tag to your site, ideally in your base layout just before the </body> tag:

    {% load tz_detect %}
    {% tz_detect %}
  6. Add TimezoneMiddleware to MIDDLEWARE:

    import django
    
    MIDDLEWARE += (
        'tz_detect.middleware.TimezoneMiddleware',
    )
    
    if django.VERSION < (1, 10):
        MIDDLEWARE_CLASSES += (
            'tz_detect.middleware.TimezoneMiddleware',
        )
  7. (Optional) Configure the countries in which your app will be most commonly used:

    # These countries will be prioritized in the search
    # for a matching timezone. Consider putting your
    # app's most popular countries first.
    # Defaults to the top Internet using countries.
    TZ_DETECT_COUNTRIES = ('CN', 'US', 'IN', 'JP', 'BR', 'RU', 'DE', 'FR', 'GB')

Please see example application. This application is used to manually test the functionalities of this package. This also serves as a good example.

You need only Django 1.8 or above to run that. It might run on older versions but that is not tested.

Caveats

  • Django's timezone awareness will not be available on the first page view
  • This method requires JavaScript
  • Timezone detection is done entirely from the user's GMT offset, not from their location

Future expansion

  • A hook to allow the timezone to be stored against a user
  • Allow timezones to be manually specified
  • Improve timezone detection
  • Optionally using HTML5's location API for better timezone determination

django-tz-detect's People

Contributors

bashu avatar adamcharnock avatar pirandig avatar ericdwang avatar blag avatar qnub avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.