如何在Django中使用get_next_by_FOO()?
我正在建立博客网站,并尝试分别为下一篇文章和上一篇文章放置 next
和 prev
按钮.
I am building blog website and trying to put next
and prev
buttons for next post and previous post respectively.
在正式文档中,它解释了 get_next_by_FOO(** kwargs)
和,其中FOO是字段名称.这将返回有关日期字段的下一个和上一个对象
.
In the official document, it explains get_next_by_FOO(**kwargs)
and 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正在关注.
So my models.py and views.py are following.
models.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)
我误解了它的概念吗?如果这样做,我该如何处理?提前致谢!
Do I misunderstand its concept?? If I do, how can I deal with it? Thanks in advance!
get_next_by_FOO
在date字段上工作,就像让我根据日期(或datetime)字段排序的下一条记录) FOO ".
get_next_by_FOO
works on the date field, think of it like "get me the next record ordered by the date (or datetime) field FOO".
所以 FOO 是日期或日期时间字段的名称.
So FOO is the name of a date or datetime field.
在模型中,您可以说根据时间戳获取下一条记录",这可以是 get_next_by_timestamp()
或根据更新日期获取下一条记录",并且这将是 get_next_by_updated()
.
In your model, you can say "get me the next record based on timestamp", and this would be get_next_by_timestamp()
or "get me the next record based on the updated date", and this would be get_next_by_updated()
.