Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 发动机分页_Python_Mongodb_Flask_Mongoengine - Fatal编程技术网

Python 发动机分页

Python 发动机分页,python,mongodb,flask,mongoengine,Python,Mongodb,Flask,Mongoengine,我有一个小烧瓶应用程序,可以呈现博客帖子: 视图。py: class ListView(MethodView): def get(self, page=1): posts = Post.objects.all() return render_template('posts/list.html', posts=posts) 这很好,但是我想在posts对象中添加分页。看一下,我看到有一个分页类 所以我试了一下: class ListView(Method

我有一个小烧瓶应用程序,可以呈现博客帖子:

视图。py:

class ListView(MethodView):

    def get(self, page=1):
        posts = Post.objects.all()
        return render_template('posts/list.html', posts=posts)
这很好,但是我想在
posts
对象中添加分页。看一下,我看到有一个分页类

所以我试了一下:

class ListView(MethodView):

    def get(self, page=1):
        posts = Post.objects.paginate(page=page, per_page=10)
        return render_template('posts/list.html', posts=posts)
但现在我得到一个错误:

TypeError: 'Pagination' object is not iterable

那么,如何在模板中迭代我的
帖子

分页
对象具有一个包含mongoengine文档对象(在您的例子中是
帖子
对象)的属性。此列表可以循环显示文档

例如,在模板中:

{% for post in posts.items %}
    {{ post.title }}
    {{ post.content }}
{% endfor %}
要获取分页链接的实际页码,请使用:


{%用于posts.iter_pages()%}
{{page}
{%endfor%}
和都有一个更好的分页链接示例:

{% macro render_pagination(pagination, endpoint) %}
    <div class=pagination>
        {%- for page in pagination.iter_pages() %}
            {% if page %}
                {% if page != pagination.page %}
                    <a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
                {% else %}
                    <strong>{{ page }}</strong>
                {% endif %}
            {% else %}
                <span class=ellipsis>…</span>
            {% endif %}
        {%- endfor %}
    </div>
{% endmacro %}
{%macro render_分页(分页,端点)%}
{%-用于分页中的页面。iter_pages()%}
{%if page%}
{%if page!=pagination.page%}
{%else%}
{{page}
{%endif%}
{%else%}
…
{%endif%}
{%-endfor%}
{%endmacro%}

您的模板代码是什么?您可以共享吗?更新的github链接以反映最新的flask mongoengine版本
{% macro render_pagination(pagination, endpoint) %}
    <div class=pagination>
        {%- for page in pagination.iter_pages() %}
            {% if page %}
                {% if page != pagination.page %}
                    <a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
                {% else %}
                    <strong>{{ page }}</strong>
                {% endif %}
            {% else %}
                <span class=ellipsis>…</span>
            {% endif %}
        {%- endfor %}
    </div>
{% endmacro %}