previous_index 和 next_index 是灰色

来源:4-6 实现上下篇文章跳转

好好敲代码

2021-09-16 13:33

def get_detail_page(request, article_id):
    all_article = Article.objects.all()
    curr_article = None
    previous_index = 0
    next_index = 0
    previous_article = None
    next_article = None
    for index, article in enumerate(all_article):
        if index == 0:
            previous_index = 0
            next_index = index + 1
        elif index == len(all_article) - 1:
            previous_index = index - 1
            next_index = index
        else:
            previous_index = index - 1
            next_index = index + 1
        if article.article_id == article_id:
            curr_article = article
            previous_article = all_article[previous_index]
            next_article = all_article[next_index]
            break

    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
                  }
                  )

http://img2.mukewang.com/6142dada00013f1107300691.jpg

写回答 关注

1回答

  • 帝国大学
    2022-08-24 11:11:39

    不影响正常功能,可以将 if语句中的 "previous_index = 0"删除

三小时带你入门Django框架 新版上架

Python开发上手Web框架的必备课程,三小时带你入门Django框架。

25587 学习 · 248 问题

查看课程

相似问题