我现在正在读《Django by Example》一书。
我在使用参数查找记录时遇到问题。
我的代码如下所示:
设置.py
TIME_ZONE = 'Asia/Seoul'
模型.py
...
published = models.DateTimeField(default=timezone.now)
...
views.py
def post_show(request, year, month, day, slug):
post = get_object_or_404(Post,
slug=slug,
status='published',
published__year=year,
published__month=month,
published__day=day)
return render(request, 'blog/default/post/show.html', {'post': post})
urls.py
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$',
views.post_show, name='post_show'),
MVT 工作正常,但我认为 DateTimeField、TimeZone 或 SQLite3 有问题。
在 SQLite3 中,“published”DateTimeField 的值为:“2016-05-17 19:57:03”,这是 UTC 时间。我提前 9 小时到达亚洲/首尔。所以我实际上是在 5 月 18 日凌晨 4:57 发布的。
>>> p = get_object_or_404(Post,slug='test', published__year=2016, published__month=5, published__day=18)
>>> p.title
'test'
>>> p.published
datetime.datetime(2016, 5, 17, 19, 57, 3, tzinfo=<UTC>)
DB说17号发布,但我必须传递参数“18”。如果我通过了 17,它就会抛出 404。
如何强制过滤条件使用 UTC 时区?