【发布时间】:2016-12-05 20:50:25
【问题描述】:
我正在按照教程创建博客。 根据教程,代码是正确的。 唯一的区别是我使用 Django 1.9 而不是 1.8
在视图中调用Post模型而不用
publish__year=year,
publish__month=month,
publish__day=day)
不返回 404 错误 - No Post matches the given query.
这是view.py
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post,
status='published',)
#publish__year=year,
#publish__month=month,
#publish__day=day)
return render(request, 'blog/post/detail.html', {'post': post})
模型部分看起来像models.py
class Post(models.Model):
...
publish = models.DateTimeField(default = timezone.now)
...
任何想法为什么找不到查询?
编辑:
网址看起来像localhost/blog/2016/07/30/second-post-entry/
def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.publish.year,
self.publish.strftime('%m'),
self.publish.strftime('%d'),
self.slug])
似乎是这些问题:
self.publish.strftime('%m'), # eg. == 07, but publish__month == 7
self.publish.strftime('%d') # eg. == 30, publish__day == 30
【问题讨论】:
-
如果您得到
No Post matches the given query,则表示没有与该年、月和日匹配的帖子。我们不知道您的数据库中有哪些帖子,发布日期是什么,或者您尝试访问的网址是什么,因此我们无法告诉您除此之外还有什么问题。 -
@Alasdair URL 的结构是我在我的数据库中找到的:
print(posts[1].publish) 2016-07-30 17:47:28+00:00 -
好的。 7 != '07' self.publish.strftime('%m') != publish__month ... 那么如何用 0 调用月份呢?现在正确的方法是什么?
-
尝试转换为整数,例如
publish__month=int(month)