Fixed #21487 -- Session cannot store tzinfo instances anymore.
Thanks filipp for the report.
Forward-port of 1eddca0a
from stable/1.6.x.
This commit is contained in:
parent
4a54b69e88
commit
9f8810ac51
|
@ -184,24 +184,25 @@ error handling entirely for the sake of simplicity.)
|
||||||
|
|
||||||
Add the following middleware to :setting:`MIDDLEWARE_CLASSES`::
|
Add the following middleware to :setting:`MIDDLEWARE_CLASSES`::
|
||||||
|
|
||||||
|
import pytz
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
class TimezoneMiddleware(object):
|
class TimezoneMiddleware(object):
|
||||||
def process_request(self, request):
|
def process_request(self, request):
|
||||||
tz = request.session.get('django_timezone')
|
tzname = request.session.get('django_timezone')
|
||||||
if tz:
|
if tzname:
|
||||||
timezone.activate(tz)
|
timezone.activate(pytz.timezone(tzname))
|
||||||
else:
|
else:
|
||||||
timezone.deactivate()
|
timezone.deactivate()
|
||||||
|
|
||||||
Create a view that can set the current timezone::
|
Create a view that can set the current timezone::
|
||||||
|
|
||||||
import pytz
|
|
||||||
from django.shortcuts import redirect, render
|
from django.shortcuts import redirect, render
|
||||||
|
|
||||||
def set_timezone(request):
|
def set_timezone(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
request.session['django_timezone'] = pytz.timezone(request.POST['timezone'])
|
request.session['django_timezone'] = request.POST['timezone']
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
else:
|
else:
|
||||||
return render(request, 'template.html', {'timezones': pytz.common_timezones})
|
return render(request, 'template.html', {'timezones': pytz.common_timezones})
|
||||||
|
|
Loading…
Reference in New Issue