Python Flask SQLAlchemy 分页导航栏出现None这个页码?

在爬取网页信息后(爬虫代码没有发出来)将结果存入Mysql数据库,并用flask发布显示,提取显示部分的代码如下:

class all_News(db.Model):
    __tablename__ = 'base_news'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    link = db.Column(db.String(200), nullable=False)
    read_num = db.Column(db.Integer, nullable=False)
    date = db.Column(db.String(200),nullable=False)

    def __repr__(self):
        return '<News %r>'%self.title

@app.route('/')
def index():
    return render_template('lol_index.html')

@app.route('/all')
@app.route('/all/<int:page>/')
def all(page=None):
    if page is None:
        page=1
    news_list=all_News.query.order_by(all_News.date.desc()).paginate(page=page,per_page=8)
    return render_template('lol_all.html',news_list=news_list,page=page)

if __name__=='__main__':
    app.run(debug=True)
    

其中lol_all.html代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>LOL所有新闻</title>
</head>
<body>

    {% for news in news_list.items %}
    <div>{{news.id}}:{{news.title}}&nbsp&nbsp&nbsp发布时间:{{news.date}}</div>
    {% endfor %}

<nav>
  <ul class="pagination">
    {% if news_list.has_prev %}
    <li style="display:inline;"><a href="{{ url_for('all', page=news_list.prev_num) }}">上一页</a></li>
    {% else %}
    <li style="display:inline;" class="disabled"><a href="###">没有上一页了</a></li>
    {% endif %}
    {% for page in news_list.iter_pages() %}
    {% if page == news_list.page %}
    <li style="display:inline;" class="active"><a href="###">{{ page }}</a></li>
    {% else %}
    <li style="display:inline;"><a href="{{ url_for('all', page=page) }}">{{ page}}</a></li>
    {% endif %}
    {% endfor %}
    {% if news_list.has_next %}
    <li style="display:inline;"><a href="{{ url_for('all', page=news_list.next_num ) }}">下一页</a></li>
    {% else %}
    <li style="display:inline;" class="disabled"><a href="###">没有下一页了</a></li>
    {% endif %}
  </ul>
</nav>
</body>
</html>

但最后中间几页的分页结果类似于这样:
上一页 1 2 None 6 7 8 9 下一页
上一页 1 2 None 5 6 7 8 9 下一页

请问我该如何修改使分页样式正确呢?

阅读 3k
2 个回答

修改成下面的内容试试:

<nav>
  <ul class="pagination">
    {% if news_list.has_prev %}
    <li style="display:inline;"><a href="{{ url_for('all', page=news_list.prev_num) }}">上一页</a></li>
    {% else %}
    <li style="display:inline;" class="disabled"><a href="###">没有上一页了</a></li>
    {% endif %}
    {% for page in news_list.iter_pages() %}
        {% if page %}
            {% if page == news_list.page %}
            <li style="display:inline;" class="active"><a href="###">{{ page }}</a></li>
            {% else %}
            <li style="display:inline;"><a href="{{ url_for('all', page=page) }}">{{ page}}</a></li>
            {% endif %}
        {% else %}
            <li class="disabled"><a href="#">&hellip;</a></li>
        {% endif %}
    {% endfor %}
    {% if news_list.has_next %}
    <li style="display:inline;"><a href="{{ url_for('all', page=news_list.next_num ) }}">下一页</a></li>
    {% else %}
    <li style="display:inline;" class="disabled"><a href="###">没有下一页了</a></li>
    {% endif %}
  </ul>
</nav>
</body>
</html>

类似于这样的结果,不好吗??
图片描述

可不可以直接只使用iter_pages()的遍历功能,然后内部使用{{ loop.index }}显示页码,或者变量的形式显示页码。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题