【发布时间】:2016-07-21 10:05:07
【问题描述】:
我的 django 应用程序有问题,我创建了两个博客对象,一个在 mysql 中,另一个在我的视图中。我在我的数据库中都看到了它们,但是当我通过查询获取它们时却看不到它们。
我正在使用 django 1.9、python 2.7、apache 2.2 和 mod_wsgi
这是我的观点和模板。
def list_view(request):
blogs = Blog.objects.filter(published=True)
return render_to_response('blog/list.html',
{
"blogs": blogs,
},
context_instance=RequestContext(request))
{% for blog in blogs %}
<h2>{{blog.title}}</h2>
<p>{{blog.content}}</p>
<span>{% trans 'Writen by' %} : {{blog.writer.last_name}} {{blog.writer.first_name}}</span> - <span>{% trans 'Published on' %} {{blog.date_published}}</span>
{% endfor %}
查询给我一个列表,里面有 2 个博客对象,但它们是空的。我的模板只显示 Writen By - Published on 两次。
但我可以登录并打印我所有的用户信息。
知道可能是什么问题或我该如何解决?非常感谢!
编辑:添加models.py
class Blog(models.Model):
title = models.CharField(_("Title"), max_length=255, null=False, blank=False)
content = models.TextField(_("Content"), null=True, blank=True)
writer = models.ForeignKey(User, verbose_name=_('Writer'), blank=False, null=False)
published = models.BooleanField(_("Pusblished"), default=False)
date_published = models.DateTimeField(_("Date published"))
def __str__(self):
return '%s' % (self.title)
def __init__(self, *args, **kwargs):
super(Blog, self).__init__()
【问题讨论】:
-
您的博客模型有标题、内容和作者属性吗?显示代码。
-
我已经添加了我的 models.py
标签: python mysql django apache mod-wsgi