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.

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
  1. Add tz_detect to your INSTALLED_APPS:

INSTALLED_APPS = (
    'tz_detect',
)
  1. Update your urls.py file:

urlpatterns = patterns('',
    url(r'^tz-detect/', include('tz_detect.urls')),
    ...
)
  1. Add the detection template tag to your site, ideally in your base layout just before the </body> tag:

    {% load tz_detect %}
    {% tz_detect %}
  2. Add TimezoneMiddleware to MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
    # Django defaults
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # For tz_detect
    'tz_detect.middleware.TimezoneMiddleware',
 )
  1. (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_COUNTIRES = ('CN', 'US', 'IN', 'JP', 'BR', 'RU', 'DE', 'FR', 'GB')

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.1.0.tar.gz (5.5 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for django-tz-detect-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3d70c1f6336620340442e5a21d7a83e0097a740a41ce3787755ba4fd921b7dd1
MD5 7310c2678bea6a20809920a6655c0e6e
BLAKE2b-256 c5c3df1ae3938d5791f1f895a8285dd9672c1d4996d85b83743e9426ffe0bc65

See more details on using hashes here.

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