def get_details_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/details.html', { 'curr_article': curr_article, 'section_list': section_list, 'previous_article': previous_article, 'next_article': next_article, } )
<li><a href="/blog/detail/{{ previous_article.article_id }}">上一篇:{{ previous_article.article_title }}</a></li> <li><a href="/blog/detail/{{ next_article.article_id }}">下一篇:{{ next_article.article_title }}</a></li>
如上代码检查多次,但显示依然如图所示 :
在django shell测试了enumerate(),结果也正常,从0到9
所以问题出现在哪里,望老师和其他同学指教一下,谢谢
经过和老师代码的比对,多次检查,发现了问题的所在:
previous_index
=
=
0
next_index
=
=
index
+
1
在写这里代码时,使用了 "=="符号,改为“=”符号后运行正常,可以正常显示和跳转上下文了。