This chapter is accompanied by video tutorials :Python-Django Develop personal blog from scratch _ Bili, Bili _bilibili
Content based websites generally implement the functions of the previous article and the next article at the bottom of the article details , This is also a general function , There are two ways to achieve it , One is through articles id To achieve , One is based on the release time of the article !
One 、 Through the article id To realize the first and second parts
stay views.py In the document post_detail Add to view function
prev_post = Post.objects.filter(id__lt=post_id).last() # Last one
next_post = Post.objects.filter(id__gt=post_id).first() # Next
Two 、 Through the release time to achieve the next chapter
stay views.py In the document post_detail Add to view function
date_prev_post = Post.objects.filter(add_date__lt=post.add_date).last()
date_next_post = Post.objects.filter(add_date__gt=post.add_date).first()
The complete code is as follows :
def post_detail(request, post_id):
# Article details page
post = get_object_or_404(Po
2021 year 6 month Python Analy