简体   繁体   English

如何使Django为系统区域设置使用正确的当前时间?

[英]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.

相关问题 Python:无论我当前的系统时间如何,如何显示时区 IST 的当前时间 - Python: How can i show current time of timezone IST irrespective of my current system time 如何在 Django 中创建正确的正则表达式? - how can i make a correct regex in django? 如何检查当前语言环境是使用 AM/PM 还是 24 小时制? - How can I check if the current locale uses AM/PM or 24-hour time? 如何在PyMySQL中编写使用当前系统时间的查询? - How can I write a query that uses the current system time in PyMySQL? 如何让我当前的代码使用 Multithreading/concurrent.futures? - How can I make my current code use Multithreading/concurrent.futures? 我怎样才能对我的登录系统进行复杂的查询? - How can I make to make a complex query for my login system? 如何让我的 Django API 同时为多个用户工作? - How can i make my Django API work for multiple users at the same time? 我不明白如何在 django 中使用正确的 static - I can't understand how to use correct static in django 如何让我的不和谐机器人响应正确答案? - How can I make my discord bot respond to the correct answer? 我怎样才能使系统时间在 Python 中运行得更快? - How can i make the system time run faster in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM