ora*_*ge1 5 python django python-3.x
当我在管理员中看到日期和时间时,它们以UTC显示。我希望它们显示在我的本地时区。我检查了TIME_ZONE设置的文档,虽然它看起来不像是我所需的东西。TIME_ZONE确定存储在数据库中,这是不是我想设置的日期时间的时间段-我只是想localize为admin时区,但不会改变他们是如何保存在数据库级别。
有没有办法做到这一点?
tin*_*are 10
在 Django\xef\xbc\x9a 中
\n\n默认时区是TIME_ZONE 设置定义的时区。
\n\n当前时区是\xe2\x80\x99s 用于渲染的时区。
\n\n所以你应该设置“当前时区”使用:
\n\n\n\n\ndjango.utils.timezone.activate(时区)
\n
您可以在中间件中激活它(不要忘记在settings.py中将其注册到中间件):
\n\nimport pytz \nfrom django.utils import timezone\n\nclass TimezoneMiddleware:\n def __init__(self, get_response):\n self.get_response = get_response\n\n def __call__(self, request):\n timezone.activate(pytz.timezone(\'Asia/Shanghai\')\n return self.get_response(request)\nRun Code Online (Sandbox Code Playgroud)\n\n这只是一个简单的硬编码。您可以制作一个表单来选择tzinfo,将其存储在用户的配置文件中并将其加载到用户会话中,从中间件中的会话中读取tzinfo。
\n\n官方文档有相关说明:\n https://docs.djangoproject.com/en/3.0/topics/i18n/timezones/#selecting-the-current-time-zone
\n\n参考\xef\xbc\x9a
\n\nhttps://groups.google.com/forum/#!topic/django-developers/E4aiv3O6vGo
\n| 归档时间: |
|
| 查看次数: |
812 次 |
| 最近记录: |