• Stars
    star
    217
  • Rank 182,446 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 11 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy user timezone detection for Django

django-tz-detect

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

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

  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:

    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

More Repositories

1

django-hordak

Double entry accounting in Django
Python
239
star
2

django-adminlte2

Reusable AdminLTE templates and admin theme
CSS
195
star
3

lightbus

RPC & event framework for Python 3
Python
189
star
4

django-su

Login as any user from the Django admin interface, then switch back when done
Python
184
star
5

dokku-client

Heroku-style command line interface for Dokku
Python
50
star
6

seed

A utility for easily creating and releasing Python packages
Python
50
star
7

python-srtm

Python API for reading NASA's SRTM `.hgt` or `.hgt.zip` altitude files.
Python
22
star
8

django-smalluuid

Short-form UUID model & form fields for Django 1.8 and above
Python
20
star
9

battlecat

General purpose accounting interface
Python
14
star
10

django-pipeline-forgiving

An extension of the django-pipeline storage backend which forgives missing files
Python
13
star
11

swiftwind

User-friendly billing for communal households
Python
11
star
12

mate3

Python library & CLI for your Outback/Mate3s solar power system
Python
9
star
13

smalluuid

Extending Python's builtin UUID to use short UUIDs by default
Python
9
star
14

swiftwind-heroku

Swiftwind for Heroku Deployment
Python
8
star
15

python-hue-client

Python
8
star
16

factorio-status-ui

Status web interface for your factorio dedicated server. View users & download mods
Python
7
star
17

django-skeleton

CSS
4
star
18

repose

Quickly create restful API clients in Python
Python
3
star
19

django-decision-matrix

Django implementation of Weighted Attribute Matrix. For making complex decisions between multiple options. Bootstrap UI.
Python
3
star
20

i-hate-papers

Create easily readable versions of academic papers via OpenAI
Python
2
star
21

dokku-selenium

Dokku plugin for a Selenium server
Shell
2
star
22

django-encrypt

2
star
23

django-live-server

The live server test case implementation due out in Django 1.4
Python
2
star
24

flynn-utils

Python
2
star
25

django-hashed-url

Python
2
star
26

colly

Util for quick comparisons of columns in CSV files, mostly just a wrapper for 'set' built-in.
Python
1
star
27

jshumandate

A JavaScript human readable (fuzzy) date parser
1
star
28

spineapi

An experiment with Spine and Tastypie
JavaScript
1
star
29

redisoverhead

A library for calculating the overhead of Redis data
1
star