使用pytz,作为astimezone方法from pytz import timezone
time_zone = timezone(settings.TIME_ZONE)
currentTime = currentTime.astimezone(time_zone)In your Middleware:
^{pr2}$
In Your view.pyfrom django.shortcuts import redirect, render
def set_timezone(request):
if request.method == 'POST':
request.session['django_timezone'] = request.POST['timezone']
return redirect('/')
else:
return render(request, 'template.html', {'timezones': pytz.common_timezones})In your templete.html{% load tz %}
{% get_current_timezone as TIME_ZONE %}
{% csrf_token %}
Time zone:
{% for tz in timezones %}
{{ tz }}
{% endfor %}