用户注销后,Django 似乎记得上次激活的时区。
- 用户发布表单 - 表单上的日期时间解释为 UTC
- 用户使用澳大利亚/悉尼的首选时区登录
- 用户发布表单 - 表单上的日期时间解释为澳大利亚/悉尼
- 用户登出
- 用户发布表单 - 表单上的日期时间仍被解释为澳大利亚悉尼,即使 TIME_ZONE 设置为 UTC
- 重新启动服务器,然后用户(仍未登录)发布表单 - 表单上的日期时间解释为 UTC
我有
TIME_ZONE = 'UTC'
USE_TZ = True
并作为中间件:
class TimezoneMiddleware(object):
def process_request(self, request):
tz = request.session.get('django_timezone', '')
if tz:
timezone.activate(tz)
elif request.user.is_authenticated():
preferredTimezone = request.user.get_profile().preferredTimezone
timezone.activate(preferredTimezone)
我认为 Django 可能会记住上次激活的时区,如activate
源中函数的此注释中所示:
def activate(timezone):
"""
Sets the time zone for the current thread.
The ``timezone`` argument must be an instance of a tzinfo subclass or a
time zone name. If it is a time zone name, pytz is required.
"""
if isinstance(timezone, tzinfo):
_active.value = timezone
有人可以证实这一点吗?解决此问题的最佳方法是在中间件中使用 else 语句调用deactivate
吗?