我简单修改了一下 上下页的代码,逻辑可能更清晰一些
detail.html
<nav aria-label="..."> <ul class="pager"> {% if previous_article %} <li><a href="/blog/detail/{{previous_article.article_id}}">上一篇:{{previous_article.title}}</a></li> {% endif %} {% if next_article %} <li><a href="/blog/detail/{{next_article.article_id}}">下一篇:{{next_article.title}}</a></li> {% endif %} </ul> </nav>
views.py
def get_detail_page(request, article_id): curr_article = Article.objects.filter(article_id=article_id)[0] previous_article = Article.objects.filter(article_id__lt=article_id).last() next_article = Article.objects.filter(article_id__gt=article_id).first() section_list = curr_article.content.split('\n') return render(request, 'blog/detail.html', { 'curr_article': curr_article, 'section_list': section_list, 'previous_article': previous_article, 'next_article': next_article, })