【发布时间】:2016-08-22 03:03:20
【问题描述】:
我正在建立博客网站并尝试将next 和prev 按钮分别用于下一篇文章和上一篇文章。
在官方文档中,解释了get_next_by_FOO(**kwargs)和where FOO is the name of the field. This returns the next and previous object with respect to the date field。
所以我的 models.py 和 views.py 正在关注。
models.py
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
class Meta:
ordering = ["-timestamp", "-updated"]
views.py
def post_detail(request, id=None):
instance = get_object_or_404(Post, id=id)
the_next = instance.get_next_by_title()
context ={
"title": instance.title,
"instance": instance,
"the_next" : the_next,
}
return render(request, "post_detail.html", context)
我误解了它的概念吗?如果我这样做,我该如何处理? 提前致谢!
【问题讨论】: