[英]How can I make Django use the correct current time for my system locale?
From Python interpreter in the Gnome terminal on Ubuntu, I enter and get the following: 在Ubuntu的Gnome终端中的Python解释器中,我输入并获得以下内容:
>>> import datetime
>>> now = datetime.datetime.now()
>>> print now
2011-03-24 12:27:32.527229
With Django, the output to the web browser is an hour behind: 使用Django,向Web浏览器的输出会落后一个小时:
It is now 2011-03-24 11:27:38.864572.
现在是2011-03-24 11:27:38.864572。
The Django code (running on the same Ubuntu laptop) is below. 下面是Django代码(在同一台Ubuntu笔记本电脑上运行)。
From views.py: 从views.py:
from django.http import Http404, HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
And then from urls.py: 然后从urls.py中:
from django.conf.urls.defaults import *
from myapp.views import current_datetime
urlpatterns = patterns('',
(r'^current_datetime/$', current_datetime),
)
I am using the python-django package from the Ubuntu 10.04 repository (version 1.1.1-2ubuntu1.3). 我正在使用Ubuntu 10.04存储库(版本1.1.1-2ubuntu1.3)中的python-django软件包。
Any ideas why the output differs on the same machine? 任何想法为什么输出在同一台机器上不同?
您的项目中的TIME_ZONE
设置不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.