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 })
原文地址:https://www.cnblogs.com/zydeboke/p/11451662.html
时间: 2024-10-13 18:46:10