Skip to main content

django-tz-detect

https://badge.fury.io/py/django-tz-detect.svg https://img.shields.io/pypi/pyversions/django-tz-detect.svg https://img.shields.io/pypi/djversions/django-tz-detect.svg https://github.com/adamcharnock/django-tz-detect/actions/workflows/test.yml/badge.svg

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 (who is available for freelance/contract work), and some great contributors.

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 reads the browser’s timezone name (such as Europe/Amsterdam) using Intl.DateTimeFormat().resolvedOptions().timeZone, along with the UTC offset from getTimezoneOffset, and posts both back to Django.

The timezone name is stored in the user’s session. If the server doesn’t recognise the name, the offset is stored instead, and the middleware picks the closest matching timezone (see TZ_DETECT_COUNTRIES below). On each request, the middleware activates the stored timezone in Django’s timezone support.

The JavaScript will not be displayed in future requests. If the stored timezone becomes unusable, it is dropped from the session and detected again on the next page view.

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 += [
        path('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:

    MIDDLEWARE += (
        'tz_detect.middleware.TimezoneMiddleware',
    )
  7. (Optional) Configure optional settings

    Set 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')

    Set the session key that will be used to store the detected timezone

    # Session key to use, defaults to "detected_tz"
    TZ_SESSION_KEY = "my-session-key"

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

Requires Python 3.11+ and Django 5.2+.

Caveats

  • Django’s timezone awareness will not be available on the first page view

  • This method requires JavaScript

  • Timezone detection uses the browser’s timezone setting (its timezone name, or the UTC offset as a fallback), not the user’s 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

Changes

0.6.1 (2026-11-10)

  • Fixed LoginRequiredMiddleware (Django 5.1+) redirecting anonymous visitors’ timezone requests to the login page, #79.

0.6.0 (2026-11-10)

  • Now requires Python 3.11+ and Django 5.2+.

  • Migrated packaging to pyproject.toml, and added pre-commit hooks and GitHub Actions CI/release workflows.

  • Made the session key configurable with the TZ_SESSION_KEY setting, #71.

  • Fixed timezone names containing + (such as Etc/GMT+3) being sent to the server as Etc/GMT 3, #70, #75.

  • Fixed an invalid or unknown timezone in the session raising UnknownTimeZoneError on every request, #70, #76.

  • Replaced pytz with Python’s zoneinfo, and dropped the pytz dependency. On Django 5+, pytz zones shifted every datetime saved through forms by the zone’s LMT offset (e.g. -07:53 instead of -07:00 for America/Los_Angeles), #73, #80.

0.5.0

  • Prefer Intl timezone over offset, #63

0.4.0

  • Support Django 2.2, 3.0, 3.1, 3.2, 4.0

  • Support Python 3.7, 3.8, 3.9, 3.10

  • Drop Python 2 support

  • Switch from Travis CI to GitHub Actions

0.3.0 (2019-11-26)

  • Set attributes on script tag. (Craig Anderson)

0.2.10 (2019-03-30)

  • Dropped support for django < 1.11 (Basil Shubin)

  • Template library load: staticfiles is deprecated in favor of static (Pi Delport)

  • MIDDLEWARE_CLASSES renamed to MIDDLEWARE in Django 1.10 (Craig Anderson)

  • Add Tox configuration (Pi Delport)

  • Simplify offset_to_timezone implementation to use min() (Pi Delport)

  • Update offset_to_timezone to handle edge cases more accurately (Pi Delport)

  • Add tests for all hourly UTC offsets, with known mis-detections (Pi Delport)

0.2.9 (2017-12-18)

  • Excluding testing of django 2.0 on python 2.7 (Adam Charnock)

  • Adding django 2.0 to build matrix (Adam Charnock)

  • Implement handle of settings.CSRF_HEADER_NAME (Roman Gorbil)

  • upgraded test suite (Basil Shubin)

  • Travis config: Add Django 1.11, exclude Python 2.7/Django master (because Django 2.0 will not support Python 2.7), and allow tests against Django master to fail (Drew Hubl)

  • fixed typo, close #31 (Basil Shubin)

0.2.8 (2016-10-21)

  • dropped support for python 2.6 & 3.3 (bashu)

  • make sure example project runs under django 1.10.x (bashu)

  • Update README.rst (bashu)

  • Update .travis.yml (Eric Wang)

  • Add Django 1.10 support (Eric Wang)

0.2.7 (2016-04-28)

  • added missing VERSION file (bashu)

  • Renaming CHANGES -> CHANGES.txt (as required by seed) (bashu)

  • Updating version to be stored in VERSION file (as now done by seed) (bashu)

  • Clean up imports (Filip Figiel)

  • Don’t expect request in templatetag context. Fixes #26 (Filip Figiel)

0.2.6 (2016-02-13)

  • fix for Django 1.10 (kudos to @PetrDlouhy)

  • Added django 1.9 support (Basil Shubin)

0.2.5 (2015-05-06)

  • python 3 support (Basil Shubin)

0.2.4 (2015-05-06)

  • Handle existing sessions storing basestring objects (Basil Shubin)

0.2.3 (2015-05-24)

  • Correctly handle NonExistentTimeError exception. (Basil Shubin)

  • Support django 1.6+ default session serializer, JSONSerializer (Doug Cox)

  • Backward compatibility with django 1.4 (Basil Shubin)

0.2.2 (2013-08-31)

  • Fixing typo in setting name. Thanks to @ustun for noticing. (Adam Charnock)

0.2.1 (2013-08-30)

  • Updating setup.py to use setuptools exclusively (as per recent seed changes) (Adam Charnock)

0.2.0 (2013-08-30)

  • Correcting JS error (for issue #2) (Adam Charnock)

  • Work on issue #2 - Disable posting timezone to server if cookies are disabled (Adam Charnock)

  • Adding badges to readme (Adam Charnock)

0.1.5 (2013-06-26)

  • updating manifest (Adam Charnock)

  • remove offending line (Rich Atkinson)

  • insert new script next to another script, not just inside opening <html> (Rich Atkinson)

0.1.4 (2013-06-19)

  • Fixing setup.py packages directive (Adam Charnock)

0.1.3 (2013-06-19)

  • Fixing setup.py packages directive (Adam Charnock)

0.1.2 (2013-06-19)

  • Fixing setup.py packages directive (Adam Charnock)

0.1.1 (2013-06-19)

  • Fixing setup.py packages directive (Adam Charnock)

0.1.0 (2013-06-19)

  • First version

Metadata

Release files for django-tz-detect 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-tz-detect 0.6.1
File Size Uploaded Provenance Actions
django_tz_detect-0.6.1.tar.gz 12.2 kB With provenance Details for django_tz_detect-0.6.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-tz-detect 0.6.1
File Interpreter ABI Platform Actions
django_tz_detect-0.6.1-py3-none-any.whl Python 3 none any Details for django_tz_detect-0.6.1-py3-none-any.whl

Total release size: 25.4 kB

Release files / django_tz_detect-0.6.1.tar.gz

Download URL django_tz_detect-0.6.1.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
44a3eeca4f2d740dc024364f6e846c951d561f6dd8244067bfbd5af688457b15
BLAKE2b-256 checksum
How to use checksums
ea5e7f2881bb4ab0022db29557294a91322748e33c6620f0c2c6d1b0ac0f1bf8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / django_tz_detect-0.6.1-py3-none-any.whl

Download URL django_tz_detect-0.6.1-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04dd02bad0468ea88db8b97c31e44f6dc4d9d13c24db06ca1ecd29c74f2efaac
BLAKE2b-256 checksum
How to use checksums
2e7a0d1deddc9116be2a22ebaf2c87ba5eaec99016060d20a492ea603a79bf98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for django-tz-detect 0.6.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_tz_detect-0.6.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page