Skip to main content

Automatic user timezone detection for django

Project description

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://img.shields.io/pypi/v/django-tz-detect.svg https://img.shields.io/pypi/dm/django-tz-detect.svg https://img.shields.io/github/license/adamcharnock/django-tz-detect.svg https://img.shields.io/travis/adamcharnock/django-tz-detect.svg 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.core.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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-tz-detect-0.2.8.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

django_tz_detect-0.2.8-py2.py3-none-any.whl (13.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-tz-detect-0.2.8.tar.gz.

File metadata

File hashes

Hashes for django-tz-detect-0.2.8.tar.gz
Algorithm Hash digest
SHA256 79f84b09bc6ed21d06329ab1d7cbbb7fa4b7ae707c33b4ebe335e55f52418785
MD5 040a06cbcafa49f8df7a873b286f76e7
BLAKE2b-256 54afaff56ef4d3c27a9f63e63564bd1de1177c5a926c03a6631887aee6102da8

See more details on using hashes here.

Provenance

File details

Details for the file django_tz_detect-0.2.8-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_tz_detect-0.2.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f001e31bd6b5b4da0efd0a17f6365a8154cb4873449db340c1aa37002d3ab549
MD5 dc861a4b6ef7028e30ef155936aeae3b
BLAKE2b-256 a82b1715e197c08d506265f457009ebefba133b5b9b88871f9d92a67a0970f4f

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page