首页 > 解决方案 > 使用 flask_paginate (pymongo) 实现分页

问题描述

我想使用 flask_paginate 实现烧瓶分页。我正在使用 MongoDB

我已经制定了这条“家”路线来实现这一点。但它不起作用。

这是我的家乡路线代码

@app.route("/")
@app.route("/home")
def home():
if current_user.is_authenticated:
    search = False
    q = request.args.get('q')
    if q:
        search = True

    page = request.args.get(get_page_parameter(), type=int, default=1)

    posts=mongo.db.articles
    #allpost=posts.find().limit(5)
    it=current_user.user_json['interest']
    allpost=posts.find( {'NewsType': it } )
    pagination = Pagination(page=page,per_page=5 ,total=allpost.count(), search=search, record_name='allpost')

    #flash(session['email'])        
    return render_template('index.html', posts=allpost,pagination=pagination)
else:
    return redirect(url_for('login'))
    #return render_template('login.html', title='Login',form=LoginForm())        

{% extends "layout.html" %}
{% block content %}
{% for post in posts %}
    <article class="media content-section">
      <div class="media-body">
        <div class="article-metadata">
         <a class="mr-2" href="#">{{ post.email }}</a>
         <small class="text-muted">{{ post.Date }}</small>
        </div>
        <h2><a class="article-title" href="{{url_for('post',post_id=post._id)}}">{{ post.Heading }}</a></h2>
        <p class="article-content">{{ post.NewsType }}</p>
      </div>
    </article>
{% endfor %}
{% endblock content %}
{{ pagination.links }}

这是我正在使用的 index.html 文件

我想要每页 5 篇文章。有人可以告诉我为什么这段代码不起作用,因为我可以转到下一页,但它显示所有文章而不是 5

标签: pythonmongodbflaskpymongo

解决方案


推荐阅读