3.5实现上下篇文章跳转

Posted _simpleSmile

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.5实现上下篇文章跳转相关的知识,希望对你有一定的参考价值。

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实现上下篇文章跳转的主要内容,如果未能解决你的问题,请参考以下文章

markdown [Apereo CAS 3.5 CORE] Apereo CAS 3.5 #CAS的核心代码片段

ctags-vim代码间快速跳转

上下文菜单不适用于两个片段

JavaScript笔试题(js高级代码片段)

python使用上下文对代码片段进行计时,非装饰器

片段无法转换为上下文