[PYTHON] Display the time in Django according to the user's region (UTC)

Use django-tz-detect . (The source code is here) An open source library released in 2019 with the MIT license applied.
Execution environment
Python:3.7.4 Django:2.2.17

1. 1. Library installation

pip install django-tz-detect

2. 2. Edit setting.py

Add the following to the end of INSTALLED_APPS

setting.py



INSTALLED_APPS (
    'tz_detect',#add to
)

Check if TEMPLATES has 'django.template.context_processors.request' (add if not)

setting.py


TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'django.template.context_processors.request',#If not, add
            ],
        },
    },
]

Added tz_detect.middleware.TimezoneMiddleware to the end of MIDDLEWARE

setting.py



MIDDLEWARE = (
    'tz_detect.middleware.TimezoneMiddleware',#add to
)

3. 3. add url

urls.py


urlpatterns = [
    url(r'^tz_detect/', include('tz_detect.urls')),#add to
]

4. HTML editing

Add {% load tz_detect%} at the beginning and {% tz_detect%} inside the body tag

At this time, it is desirable to describe {% tz_detect%} immediately before \ because of reading.

base.html


{% load tz_detect %}
<head></head>
<body>
{% tz_detect %}
</body>

Change the time zone setting and raise the browser again to complete. (It may take 1 to 2 minutes to reflect)

Recommended Posts

Display the time in Django according to the user's region (UTC)
django timezone and language settings
Display the time in Django according to the user's region (UTC)
I tried to illustrate the time and time in C language
I tried to display the time and today's weather w
Django ~ Let's display it in the browser ~
I want to display the progress in Python!
Set the form DateField to type = date in Django
Display / update the graph according to the input with PySimpleGui
[Django] How to redirect unlogged-in users to the login page
Python OpenCV tried to display the image in text.
How to write custom validations in the Django REST Framework
Dot according to the image
Summary of stumbling blocks in Django for the first time
Steps to change table and column names in your Django model at the same time
I tried to illustrate the time and time in C language
How to generate a query using the IN operator in Django
I tried to display the time and today's weather w
[Python] Display the elapsed time in hours, minutes, and seconds (00:00:00)
I can't log in to the admin page with Django3
In Django, how to abbreviate the long displayed string as ....
The problem with having to specify SameSite = None in chrome80, what are all django users doing?
The story of returning to the front line for the first time in 5 years and refactoring Python Django
I tried to describe the traffic in real time with WebSocket
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
I tried to display the video playback time (OpenCV: Python version)
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
[Django] Display registration data associated with users on the registration / edit form (Form)
I made a command to display a colorful calendar in the terminal
How to display bytes in the same way in Java and Python
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Errors related to memcached in django
Programming to fight in the world-Chapter 4
In the python command python points to python3.8
How to reflect CSS in Django
Switch the language displayed in Django 1.9
Deploy the Django tutorial to IIS ①
The meaning of ".object" in Django
Cython to try in the shortest
Start Django for the first time
Programming to fight in the world ~ 5-2
Get the query string (query string) in Django
I want to use the Django Debug Toolbar in my Ajax application
Added a function to register desired shifts in the Django shift table
For the first time in Numpy, I will update it from time to time
I tried to display the altitude value of DTM in a graph
How to display the progress bar (tqdm)
Get the client's IP address in Django
How to suppress display error in matplotlib
How to check the version of Django
Set the time zone to Japan Standard Time
How to set the server time to Japanese time
How to delete expired sessions in Django
Display Python 3 in the browser with MAMP
Easy conversion from UTC to local time
How to display multiplication table in python
In Jupyter, add IPerl to the kernel.
MongoDB for the first time in Python
I want to display the progress bar
Django non-logged-in users want to transition to login.html
Various comments to write in the program