码迷,mamicode.com
首页 > 其他好文 > 详细

【3.5】实现上下篇文章跳转

时间:2019-09-03 12:06:15      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:pre   class   nav   article   第一篇   span   rev   实现   none   

1.添加上下篇文章的按钮

1 <div>
2         <nav aria-label="...">
3             <ul class="pager">
4                 <li><a href="/blog/detail/{{ previous_article.article_id }}">上一篇:{{ previous_article.title }}</a></li>
5                 <li><a href="/blog/detail/{{ next_article.article_id }}">下一篇:{{ next_article.title }}</a></li>
6             </ul>
7         </nav>
8     </div>

 

2.修改视图函数

 1 def get_detail_page(requset, article_id):
 2     all_article = Article.objects.all()
 3     curr_article = None
 4     previous_index = 0
 5     next_index = 0
 6     previous_article = None
 7     next_article = None
 8     for index, article in enumerate(all_article):
 9         # 当文章是第一篇的时候
10         if index == 0:
11             previous_index = 0
12             next_index = index + 1
13         # 当文章是最后一篇的时候
14         elif index == len(all_article) - 1:
15             previous_index = index - 1
16             next_index = index
17         else:
18             previous_index = index - 1
19             next_index = index + 1
20         if article.article_id == article_id:
21             curr_article = article
22             previous_article = all_article[previous_index]
23             next_article = all_article[next_index]
24             break
25     section_list = curr_article.content.split(\n)
26     return render(requset, blog/detail.html,
27                   {
28                       curr_article: curr_article,
29                       section_list: section_list,
30                       previous_article: previous_article,
31                       next_article: next_article
32                   })

 

【3.5】实现上下篇文章跳转

标签:pre   class   nav   article   第一篇   span   rev   实现   none   

原文地址:https://www.cnblogs.com/zydeboke/p/11451662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!