当前位置:网站首页>Paging of the flask page
Paging of the flask page
2022-06-11 07:34:00 【start field】
Paging is a very common function , Especially when the data reaches a certain amount , If it's all on the page , It will make the page too long and affect the user experience , besides , There may also be problems such as slow loading . therefore , Paging is necessary .
This is the rendering of paging , The data are all from MySQL From the database .

Paging code
from flask import Flask,render_template,request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# Connect to database
HOSTNAME = '127.0.0.1'
PORT = '3306'
DARABASE = ''
USERNAME = ''
PASSWORD = ''
DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME,PASSWORD,HOSTNAME,PORT,DARABASE)
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
@app.route('/movie')
def movie():
db.reflect()
# Get all data tables
all_table={table_obj.name: table_obj for table_obj in db.get_tables_for_bind()}
# location movies surface
data=db.session.query(all_table['movies'])
# The current page number , Start with the first page
page = int(request.args.get("page", 1))
# The number of pages
per_page = int(request.args.get('per_page', 25))
paginate = data.paginate(page, per_page, error_out=False)
movies = paginate.items
return render_template("movie.html",movies=movies, paginate=paginate)paginate It's a SQLAlchemy Medium Pagination Type object .paginate Method passed in two parameters , One page It's the current page , the other one per_page Is the maximum number of pieces of data displayed on each page .
The front-end code
{% if paginate %}
<ul class="pagination pagination-sm no-margin pull-right">
<li><a href="/movie?page=1"> home page </a></li>
{% if paginate.has_prev %}
<li class="active"><a href="/movie?page={
{ paginate.prev_num }}"> The previous page </a></li>
{% else %}
<li class="disabled"><a href=""> The previous page </a></li>
{% endif %}
{% for v in paginate.iter_pages(1,1,3,1) %} <!--iter_pages It's an iterator Returns the number of pages on the page bar If it doesn't display return None-->
{% if v %} <!--iter_pages(left_edge=2,left_current=2,right_current=5,right_edge=2) There are default parameters , Left edge display 2 individual , The right edge shows 2 individual , The left side of the current page number displays 2 individual , The right side of the current page number displays 5 individual -->
{% if v==paginate.page %}
<li class="active"><a href="#">{
{ v }}</a></li> <!-- Displays the current number of pages -->
{% else %}
<li><a href="/movie?page={
{ v }}">{
{v}}</a></li> <!-- Displays the number of pages other than the current number -->
{% endif %}
{% else %}
<li class="disabled"><a href="">…</a></li> <!-- Show None Become an ellipsis -->
{% endif %}
{% endfor %}
{% if paginate.has_next %}
<li class="active"><a href="/movie?page={
{ paginate.next_num }}"> The next page </a></li>
{% else %}
<li class="disabled"><a href=""> The next page </a></li>
{% endif %}
<li><a href="/movie?page={
{ paginate.pages }}"> Tail page </a></li>
</ul>
{% endif %}Pagination The common properties of an object are :
- prev_num Previous page number
- next_num Next page number
- has_next Is there a next page True/False
- has_prev Is there a previous page True/False
- pages The total number of pages from the query
- total The total number of records
iter_pages It's an iterator Returns the number of pages on the page bar If it doesn't display return None.iter_pages(left_edge=2,left_current=2,right_current=5,right_edge=2) There are default parameters , Left edge display 2 individual , The right edge shows 2 individual , The left side of the current page number displays 2 individual , The right side of the current page number displays 5 individual
Analyze the above page code :
1, On the whole, it is based on the current understanding of “ The previous page ”,“ Page number ” and “ The next page ” Make different settings
2, Use JinJa2 The macro , Definition page Method , Incoming paths and pagination Object as parameter . Generate paging content by calling macro execution .
3, utilize JinJa2 Of if Statement uses different elements depending on whether the current page has a previous page .
4, If the current page has a previous page , be pagination Object's has_prev by True, here li Labeled class by active, The previous page is clickable at this time .
5, If the current page does not have a previous page , be pagination Object's has_prev by False, here li Labeled class by disable, The previous page cannot be clicked at this time . The jump link will be #
6, And “ The previous page ” Dealing with something similar is right “ The next page ” To deal with . If the current page has a next page, set the style and click jump path , If there is no next page , Then it is set as non clickable and the jump path is #
7, utilize iter_pages(1,1,3,1) You will get a list of page numbers that should be displayed based on the current page . As shown in the screenshot above , If the current page is page 4 page , The contents of the paging list obtained are [1,None,3,4,5,6,None,10], If the current page is page 10 page , The contents of the paging list obtained are [1,None,9,10]
8, utilize JinJa2 Of for Statement traverses the list , If page number , It is generated according to the page number a Label text and set jump path and add page Parameters . If it is None,a The text in the label is the ellipsis of the entity name , Path is “#”. additional , If the number corresponding to the current page number is obtained during traversal , by li Labeled class Property to add extra active To get additional styles .
Refer to the website :https://zhuanlan.zhihu.com/p/94353052
These are basic knowledge and usage , If you want to know more about pagination and its usage , Please check out Pagination Chinese Manual https://cloud.tencent.com/developer/section/1489889.
边栏推荐
- 【集群】haproxy负载均衡
- 2022低压电工考题及在线模拟考试
- Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
- 【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算
- Pointer to a two-dimensional array
- Find the longest common substring of N arrays (strings)
- Arduino_ Esp32 development record
- 【AtCoder2305】Decrementing(博弈)
- Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
- 【IoT】智能硬件:如何获取硬件产品的wifi信号强度
猜你喜欢

Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration

Import on CSDN MD file

2022 low voltage electrician test questions and online simulation test

【Oracle 数据库】奶妈式教程day03 排序查询

JVM learning record (VII) -- class loading process and parental delegation model

Qstring to hexadecimal qstring

Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please

Flask页面的分页

测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...

C language Yanghui triangle code
随机推荐
QObject usage skills -- control function class
JVM学习记录(七)——类加载过程与双亲委派模型
【软件测试】这样的简历已经刷掉了90%的面试者
Sdl-3 YUV playback
多线程复习总结之解析Volatile关键字
Miscellany C language
[analysis of STL source code] summary note (4): behind the scenes hero allocator
【AtCoder1983】BBQ Hard (组合数+巧妙模型转化)
Configuration software -- control import
RTMP protocol
QT 基于QScrollArea的界面嵌套移动
I/o multiplexing - select/poll/epoll
Pointer to a two-dimensional array
【IoT】项目管理:如何打造更好的跨职能团队?
[Oracle database] mammy tutorial day04 Sorting Query
20200803 T3 my friends [divide and conquer NTT optimization recursion]
MySQL设置管理员密码无法生效的案例一则
20200802 T3 I always like [generating function exclusion, Lagrangian inversion]
3年功能测试拿8K,被新来的反超,其实你在假装努力
Wc2020 course selection