Article From:https://segmentfault.com/q/1010000014072575
Question:

This is my flask Code:

coding=utf-8

from flask import render_template, request,flash, redirect, url_for,current_app,abort
from . import main
from ..import db
from ..models import Post, Comment
from flask_login import login_required, current_user
from .forms import CommentForm, PostForm
from flask_babel import gettext as _

@main.errorhandler(404)
def page_not_found(error):

return render_template('404.html'), 404

@main.route(‘/’)
def index():

page_index = request.args.get('page', 1, type=int)

query = Post.query.order_by(Post.created.desc())

pagination = query.paginate(page_index, per_page=20,error_out=False)

posts = pagination.items

return render_template('index.html',
                       title=_(u'Welcome to the Lee Cafe '),Posts=posts,Pagination=pagination)

This is my template code:
{% extends ‘bootstrap/base.html’ %}
{% block title %}{{ title }}{% endblock %}
{% block head %}

{{ super() }}
{% include ['includes/_head.html', 'includes/_metas.html'] %}

{% endblock %}
{% block styles %}

{{ super() }}
<link rel="stylesheet"
      href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.5/cerulean/bootstrap.min.css ">

{% endblock %}
{% block navbar %}

{# { nav.top.render() }} #}
{% include 'includes/_navbar.html' %}

{% endblock %}

{% block content %}

{% block header %}
    <header>
        <div class="page-header">
            <div class="container">
                <h1>{{ title }}</h1>
            </div>
        </div>
    </header>
{% endblock %}
{% block page_body %}
{% endblock %}

{% endblock %}

This is my mistake.

Is it the way I use it? Please answer it.

Answer 0:

The problem has been solved, and the error in pagination usage in index.html is wrong. The original code is:
{% if pagination %}

        {{ render_pagination(pagination) }}
    {% endif %}

Has been corrected:
{% if pagination %}
<div class=”pagination”>

{{ macros.pagination_widget(pagination,'.index') }}

</div>
{% endif %}

At the same time, modify the macros page template: Marco input {#% Acer (name, value=”, type=’text’, size=20)%#}
{% macro pagination_widget(pagination, endpoint, fragment=”) %}

<!--input type="{{ type }}"
       name="{{ name }}"
       value="{{ value }}"
       size="{{ size }}"
       /-->

<ul class=”pagination”>

<li{% if not pagination.has_prev %} class="disabled"{% endif %}>
    <a href="{% if pagination.has_prev %}{{ url_for(endpoint, page=pagination.prev_num, **kwargs) }}{{ fragment }}{% else %}#{% endif %}">
        &laquo;
    </a>
</li>
{% for p in pagination.iter_pages() %}
    {% if p %}
        {% if p == pagination.page %}
        <li class="active">
            <a href="{{ url_for(endpoint, page = p, **kwargs) }}{{ fragment }}">{{ p }}</a>
        </li>
        {% else %}
        <li>
            <a href="{{ url_for(endpoint, page = p, **kwargs) }}{{ fragment }}">{{ p }}</a>
        </li>
        {% endif %}
    {% else %}
    <li class="disabled"><a href="#">&hellip;</a></li>
    {% endif %}
{% endfor %}
<li{% if not pagination.has_next %} class="disabled"{% endif %}>
    <a href="{% if pagination.has_next %}{{ url_for(endpoint, page=pagination.next_num, **kwargs) }}{{ fragment }}{% else %}#{% endif %}">
        &raquo;
    </a>
</li>

</ul>

{% endmacro %}
Annotated out of the original content

Leave a Reply

Your email address will not be published. Required fields are marked *