Flask uses paginate to implement data paging (2)

1, the view function of data acquisition

# Get the user list 
@ pawnxc.route ( " / user_list " , Methods = [ " the GET " , " the POST " ])
 DEF user_list (): 
    Page = request.args.get ( ' Page ' ,. 1, type = int) 
    the pagination Pawn_dl.query.order_by = (Pawn_dl.ssfj.asc ()). the paginate (Page, per_page = 20 is, error_out = False) 
    Users = pagination.items
     # obtain a listing area 
    FJS = PawnFj.query.order_by (PawnFj.fjdm)
     # Convert query result object to dictionary data 
    ssfj_list = []
    if fjs:
        for fj in fjs:
            ssfj_list.append(fj.to_dict())
    return render_template('user_list.html', users=users, ssfj_list=ssfj_list, pagination=pagination)

Get the page number (request.args) from the request parameters. If not specified explicitly, the first page is rendered by default.

The parameter type = int guarantees that when the parameter cannot be converted to an integer, the default value is returned.

In order to display the records on a page, replace all () with the paginate () method provided by Flask-SQLAlchemy.

The number of pages is the first parameter of the paginate () method and the only required parameter.

The optional parameter per_page is used to specify the number of records displayed per page; if not specified, 20 records are displayed by default.

Another optional parameter is error_ out, when it is set to True (the default), if the requested number of pages exceeds the range, a 404 error will be returned; if set to False, an empty list will be returned when the number of pages exceeds the range .

  The return value of the paginate () method is a Pagination class object, which is defined in Flask-SQLAlchemy. This object contains many attributes that are used to generate pagination links in the template, so it is passed into the template as a parameter.

2. Pagination template (flask_pagination.html):

<!--以 Jinja2 宏的形式实现的分页导航-->
{% macro pagination_widget(pagination, endpoint) %}
<ul class="pagination">
    <li>{% if not pagination.has_prev %} {% endif %}
        <a href="{% if pagination.has_prev %}{{ url_for(endpoint,page = pagination.page - 1, **kwargs) }}
            {% 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) }}">{{ p }}</a>
    </li>
    {% else %}
    <li>
        <a href="{{ url_for(endpoint, page = p, **kwargs) }}">{{ p }}</a>
    </li>
    {% endif %}
    {% else %}
    <li class="disabled">
        <a href="#">&hellip;</a>
    </li>
    {% endif %}
    {% endfor %}
    <li>{% if not pagination.has_next %} {% endif %}
        <a href="{% if pagination.has_next %}{{ url_for(endpoint,page = pagination.page + 1, **kwargs) }}{% else %}#{% endif %}">&raquo;</a>
    </li>
</ul>
{% endmacro %}

3. Introduce this template in the HTML file that needs paging:

        {% import "flask_pagination.html" as macros %}
        {%if pagination%}
            <div class="pagination">
                {{ macros.pagination_widget(pagination, '.user_list') }}
            </div>
        {% endif %}

 

Guess you like

Origin www.cnblogs.com/hzjdpawn/p/12687127.html