好好敲代码
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 } )
不影响正常功能,可以将 if语句中的 "previous_index = 0"删除
三小时带你入门Django框架 新版上架
25628 学习 · 248 问题
相似问题