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
pip install django-tz-detect
setting.py
INSTALLED_APPS (
'tz_detect',#add to
)
'django.template.context_processors.request'
(add if not)setting.py
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...
'django.template.context_processors.request',#If not, add
],
},
},
]
tz_detect.middleware.TimezoneMiddleware
to the end of MIDDLEWAREsetting.py
MIDDLEWARE = (
'tz_detect.middleware.TimezoneMiddleware',#add to
)
urls.py
urlpatterns = [
url(r'^tz_detect/', include('tz_detect.urls')),#add to
]
{% load tz_detect%}
at the beginning and {% tz_detect%}
inside the body tagAt 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)