基于类的视图MySQL DateTimeField收到了一个幼稚的日期时间



我是Django的新手,已经到了极限,真的需要一些帮助。

我不知道如何使用"基于类的视图",并将MySQL数据库中传入的日期时间字段更改为似乎需要的时区支持条目。数据库将其存储在UTC中,而我的系统处于PST上。

我得到这个错误:

DateTimeField收到一个初始日期时间(2012-09-01 00:00:00),而时区支持为活动

在我的MonthArchiveView、DayArchiveView和DateDetailView上。出于某种原因,我的ArchiveIndexView、YearArchiveView基于类的视图工作正常。

这是我的型号:

class blogpost(models.Model):
blog_title = models.CharField(max_length=200)
blog_slug = models.SlugField(unique_for_date='blog_pub_date', max_length=200)
blog_content = models.TextField()
blog_pub_date = models.DateTimeField(default=datetime.now())
blog_category = models.ForeignKey('blogcategory')

这是我的一个观点:

class ThoughtsDetailView(DateDetailView):
template_name='thoughts/thoughts_detail.html'
queryset = blogpost.objects.all()
date_field = 'blog_pub_date'
slug_field = 'blog_slug'
context_object_name = 'thoughts_detail'
month_format = '%m'
allow_future = 'true'

下面是一个示例模板:

{% block content-inner-left %}
<h1>{{ thoughts_detail.blog_title }}</h1>
<div id="blogpost">
<p class="blogsmalldate">[ Posted on {{ thoughts_detail.blog_pub_date|date:"l, F dS, Y" }}, {{ thoughts_detail.blog_pub_time|date:"g:i a" }} ]</p>
<br />
<p>{{ thoughts_detail.blog_content|safe|linebreaks }}</p>
</div>
{% endblock content-inner-left %}

有人能帮助我了解如何修复我的每日详细信息视图,使其保持为基于类的视图吗?然后我可能会了解其他视图。我甚至尝试过使用PYTZ,但不太了解如何更改基于类的视图来使用它。谢谢。。。。

问题不在于视图,而在于日期存储在数据库中而没有时区信息,而Django是为支持时区而设置的。如果您不需要时区支持,只需在settings.py中设置USE_TZ = False;如果您这样做了,请确保数据库存储了带有时区信息的日期。有关详细信息,请访问https://docs.djangoproject.com/en/1.4/topics/i18n/timezones/#naive-和感知日期时间对象

最新更新