问答详情
源自:4-6 实现上下篇文章跳转

最后一篇文章在主页有显示,但是点击后跳转失败,其它文章正常

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blog</title>
 <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
 integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
 <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
 integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
 crossorigin="anonymous"></script>
</head>
<body>
<div class="container page-header">
    <h1>{{ curr_article.title }}</h1>
</div>
<div class="container body-main">
    <div>
 {% for section in section_list %}
            <p>{{ section }}</p>
 {% endfor %}
    </div>
</div>
<div>
    <nav aria-label="...">
        <ul class="pager">
            <li><a href="/blog/detail/{{ previous_article.article_id }}">Previous:{{ previous_article.title }}</a></li>
            <li><a href="/blog/detail/{{ next_article.article_id }}">Next:{{ next_article.title }}</a></li>
        </ul>
    </nav>
</div>


</body>
</html>


提问者:ouchaochao 2019-07-10 22:52

个回答

  • 西岛猫纪年
    2019-09-10 16:58:52

    我也是遇到了一样的问题 在内容显示判断里面加个判断 是否是最后一篇文章 再对内容进行查询

    if article.article_id == article_id:
    
        if next_index == len(all_article):
            previous_article = all_article[(previous_index-1)]
            next_article = all_article[next_index-1]
    
        else:
            previous_article = all_article[previous_index]
            next_article = all_article[next_index]
    
        curr_article = article
        break


    http://img4.mukewang.com/5d7765bd0001baff11530544.jpg

  • weixin_慕村0533635
    2019-07-17 13:47:45

    你是views.py文件里估计写的有点问题?

    for index, article in enumerate(all_article):
        if index == 0:
            previous_index = index
            next_index = index + 1
    
        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

    看我这里加粗的部分!