程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

(18) Django realizes the switching function between upper and lower articles of the blog

編輯:Python

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved