Django为什么不从数据库返回datetime字段? [英] Why isn't Django returning a datetime field from the database?

查看:408
本文介绍了Django为什么不从数据库返回datetime字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的第一个Django应用程序,我正在尝试用一个简单的报价收集站点(think bash.org),其功能非常简单,只是为了让我的脚变湿。我使用sqlite作为我的数据库,因为它是最简单的设置。

For my first Django app, I'm trying to write a simple quote collection site (think bash.org), with really simple functionality, just to get my feet wet. I'm using sqlite as my database, since it's the easiest to setup.

这是我现在唯一的模型:

Here's my only model right now:

class Quote(models.Model):
    text = models.TextField();
    upvotes = models.IntegerField(default=0)
    downvotes = models.IntegerField(default=0)
    active = models.BooleanField(default=False)
    date_published = models.DateTimeField(auto_now_add=True)

一个非常简单的详细信息模板,只是为了转储信息: p>

And a really simple detail template, just to dump the information:

Quote: {{ quote.text }}<br>
Upvotes: {{ quote.upvotes }}<br>
Downvotes: {{ quote.downvotes }}<br>
Published: {{ qoute.date_published|date:"F j, Y, g:i a" }}

当我去详细页面,给定对象的所有内容都正确输出,除了datetime(空白)外。但是,我检查了数据库,并验证了该对象列中存在一个datetime,并显示了良好的管理区域。另外,当我从manage.py运行shell时,这里是我得到的:

When I go to the detail page, everything for the given object is outputted properly, except for the datetime (blank). However, I've checked the database and verified that there is a datetime stored in that object's column, and it shows up fine admin area. Also, when I run the shell from manage.py, here's what I get:

>>> q = Quote.objects.all()[0]
>>> q.date_published
datetime.datetime(2009, 7, 24, 23, 1, 7, 858688)

此外,我使用通用视图 django.views.generic.list_detail.object_detail 来处理请求,但是我也尝试使用下面的视图,获得相同的结果。

Also, I'm using the generic view django.views.generic.list_detail.object_detail to handle the request, but I also tried to use the view below and got the same result.

def detail(Request, id):
    q = get_object_or_404(Quote, pk=id)
    return render_to_response('quotable/quote_detail.html', {'quote': q})

我在尝试显示日期时出现错误,还是在这里发生了什么?

Am I'm doing something wrong in my attempt to display the date, or is something else going on here?

谢谢。

推荐答案

正如Adam Bernier所说,你拼错报价

As Adam Bernier mentioned, you're misspelling quote

这篇关于Django为什么不从数据库返回datetime字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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