我正在關注一個教程來創建博客。 根據教程,代碼是正確的。 唯一的區別是,我使用Django 1.9,而不是1.8Django1.9:沒有模型匹配給定的查詢
調用視圖的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)
...
任何想法爲什麼查詢沒有找到?
編輯:
的URL看起來像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
如果您獲得'沒有帖子匹配給定的查詢',那意味着沒有匹配那年,每月和每天的帖子。我們不知道您在數據庫中發佈了什麼帖子,發佈日期是什麼,或者您嘗試訪問哪個網址,因此我們無法告訴您問題超出了這個範圍。 – Alasdair
@Alasdair URL的結構是我在我的數據庫中找到的:'print(posts [1] .publish) 2016-07-30 17:47:28 + 00:00' – user2853437
好的。 7!='07'self.publish.strftime('%m')!= publish__month ...那麼如何用0調用月份?現在什麼是正確的方式? – user2853437