how do I make my site timezone aware?

白昼怎懂夜的黑 提交于 2019-12-11 13:57:21

问题


I have an app that uses

import datetime

datetime.datetime.now()
default=datetime.now
auto_now=True
auto_now_add=True

I'm changing the above codes to the below.

from django.utils import timezone

timezone.now() # instead of datetime.datetime.now()
default=timezone.now # instead of datetime.now
auto_now # override save() or pre_save() .. ok don't need this change in recent django.
auto_now_add # override post_save() to set time when `created`.. don't need this change either

Am I missing something or doing something superflous?

I'm using an open source app and it uses a lot of datetime.datetime ..
It's really pain to change all that.. hope there was an easier way.


回答1:


In your settings file you have to set USE_TZ = True. From docs:

Time zone support is disabled by default. To enable it, set USE_TZ = True in your settings file



来源:https://stackoverflow.com/questions/17653369/how-do-i-make-my-site-timezone-aware

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!