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.
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
Either checkout tz_detect from GitHub, or install using pip:
pip install django-tz-detectAdd tz_detect to your INSTALLED_APPS:
INSTALLED_APPS += ( 'tz_detect', )Be sure you have the django.template.context_processors.request processor
TEMPLATES = [ { ... 'OPTIONS': { 'context_processors': [ ... 'django.template.context_processors.request', ], }, }, ]Update your urls.py file:
urlpatterns += [ path('tz_detect/', include('tz_detect.urls')), ]Add the detection template tag to your site, ideally in your base layout just before the </body> tag:
{% load tz_detect %} {% tz_detect %}Add TimezoneMiddleware to MIDDLEWARE:
import django MIDDLEWARE += ( 'tz_detect.middleware.TimezoneMiddleware', ) if django.VERSION < (1, 10): MIDDLEWARE_CLASSES += ( 'tz_detect.middleware.TimezoneMiddleware', )(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
Metadata
Release files for django-tz-detect 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-tz-detect-0.5.0.tar.gz | 11.3 kB | Without provenance | Details for django-tz-detect-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_tz_detect-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for django_tz_detect-0.5.0-py3-none-any.whl |
Total release size: 23.0 kB
Release files / django-tz-detect-0.5.0.tar.gz
| Download URL | django-tz-detect-0.5.0.tar.gz |
|---|---|
| Size | 11.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b804c562ecd6e1c2a303cc9a88e5ad18df053b07f15453d67984f3f34b038de4
|
|
BLAKE2b-256 checksum How to use checksums |
db3e75b2c2f7005d095cc23bc74a250cd2c3ff1aff232cc35dbb7e4f30084cc4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.10
|
Release files / django_tz_detect-0.5.0-py3-none-any.whl
| Download URL | django_tz_detect-0.5.0-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4dd6e5e4080c2257a205545134f252e51e2c58225de362dc8f05328c88599237
|
|
BLAKE2b-256 checksum How to use checksums |
5ee53877abeca4bb1a1fa73d040f89e6560e40206b02eb43cef9fab4efa8682b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.10
|
Security for django-tz-detect 0.5.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.