基于类的视图MySQL DateTimeField接收到一个天真的日期时间 [英] Class Based View MySQL DateTimeField received a naive datetime

查看:184
本文介绍了基于类的视图MySQL DateTimeField接收到一个天真的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Django很新,在我的绳索结尾,真的需要一些帮助。



我不知道如何使用基于类的视图,并将传入的datetime字段从我的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> [发表于{{thoughts_detail.blog_pub_date | date:l,F dS,Y}},{{thoughts_detail.blog_pub_time | date:g:ia} }]< / 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-and-aware-datetime-objects


I am very new to Django and at the end of my rope and really need some help.

I do not know how to use a "class based view" and change the incoming datetimefield from my MySQL database into a Time Zone Supported entry that it seems to need. The database stores it in UTC and my system is on PST.

I am getting this error:

DateTimeField received a naive datetime (2012-09-01 00:00:00) while time zone support is active

On my MonthArchiveView, DayArchiveView, DateDetailView 's only. For some reason my ArchiveIndexView, YearArchiveView class based views work ok.

Here is my model:

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')

Here is one of my Views:

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'

Here is an example template:

{% 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 %}

Can someone help me understand how to fix my Day Detail View so that it stays as a Class Based View and then I can probably figure out the others. I even tried to use PYTZ but don't understand enough how to change the class based view to use it. Thank you....

解决方案

The problem is not in the view, but in the fact that the dates are stored in the database without a timezone information, while Django is set up to support timezones. If you don't need timezone support, simply set USE_TZ = False in the settings.py; if you do, make sure that the database stores the dates with the timezone information. More details on that can be found at https://docs.djangoproject.com/en/1.4/topics/i18n/timezones/#naive-and-aware-datetime-objects

这篇关于基于类的视图MySQL DateTimeField接收到一个天真的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆