flask(十二)----jinjia2模版渲染

有时候我们前端的数据会随着后端的数据改变而改变,在前后端不分离的系统中,可通过jiajia2来实现模版渲染,这样前端也就更灵活。

 

1.{{  }}:实现动态模版加载

后端示例:

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def index():
    projects = [
        {"name": "项目1", "interface_num": 11, "time": "2022/3/17"},
        {"name": "项目2", "interface_num": 22, "time": "2022/3/17"},
        {"name": "项目3", "interface_num": 33, "time": "2022/3/17"},
        {"name": "项目4", "interface_num": 44, "time": "2022/3/17"},
    ]
    # render_template()中第1个参数后面的参数,会将数据传给第1个参数页面,页面中若存在{{ }},则会匹配后面参数的数据。
    return render_template('index.html', pro=projects, title='测试平台')


if __name__ == '__main__':
    app.run(debug=True)

前端示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
    <div>
        {{ pro }}
    </div>
</body>
</html>

效果:

 2.使用jinjia中的for循环,美化格式

        {% for p in projects %}

        项目:{{ p.name }} : {{ p.interfaces }}

        {% endfor %}

        后端代码不变:

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def index():
    projects = [
        {"name": "项目1", "interface_num": 11, "time": "2022/3/17"},
        {"name": "项目2", "interface_num": 22, "time": "2022/3/17"},
        {"name": "项目3", "interface_num": 33, "time": "2022/3/17"},
        {"name": "项目4", "interface_num": 44, "time": "2022/3/17"},
    ]
    # render_template()中第1个参数后面的参数,会将数据传给第1个参数页面,页面中若存在{{ }},则会匹配后面参数的数据。
    return render_template('index.html', pro=projects, title='测试平台')


if __name__ == '__main__':
    app.run(debug=True)

        前端代码修改:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
    {% for project in pro %}
    <div>项目名称:{{ project["name"] }},接口数量:{{ project["interface_num"] }},创建日期:{{ project["time"] }}</div>
    {% endfor %}
</body>
</html>

        效果:

        for循环排序:获取循环中的index: {{ loop.index }}

                loop.index:        当前循环迭代的次数,从1开始

                loop.index0:        当前循环迭代的次数,从0开始

                 loop.revindex:        当前循环的逆序次数,从最后一个index开始,直到1

                loop.revindex0:        当前循环的逆序次数,从最后一个index开始,直到0

                loop.first:        如果是第一次循环迭代,为True

                loop.last:        如果是最后一次循环迭代,为True

                loop.length:        序列中的项目数(长度)

        前端修改:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
    {% for project in pro %}
    <div>序号:{{ loop.index }}--项目名称:{{ project["name"] }},接口数量:{{ project["interface_num"] }},创建日期:{{ project["time"] }}</div>
    {% endfor %}
</body>
</html>

        效果:

        loop判断:

        {% if loop.last %}

                xxxxxx

         {% else %}

                xxxxxx

        {% endif %}

        前端代码修改

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
    {% for project in pro %}
        {% set name=project.name %} <!--# 设置变量-->
        {% if not loop.last %}
            <div>序号:{{ loop.nextitem.name }}--项目名称:{{ name }},接口数量:{{ project["interface_num"] }},创建日期:{{ project["time"] }}</div>
        {% else %}
            <div>序号:{{ loop.index }}--项目名称:{{ name }},接口数量:{{ project["interface_num"] }},创建日期:{{ project["time"] }}</div>
        {% endif %}
    {% endfor %}
</body>
</html>

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Flask-Paginate是一个Flask扩展,它提供了一种简单的方法来实现分页功能。它基于SQLAlchemy,并且可以与任何SQLAlchemy支持的数据库一起使用。 首先,你需要安装Flask-Paginate扩展。可以通过以下命令来安装: ``` pip install Flask-Paginate ``` 然后,你需要导入Flask-Paginate扩展并创建一个分页器。以下是一个简单的示例: ```python from flask_paginate import Pagination, get_page_args @app.route('/') def index(): # 获取当前页码和每页显示的数量 page, per_page, offset = get_page_args(page_parameter='page', per_page_parameter='per_page') # 从数据库中获取数据 data = get_data_from_database(offset=offset, per_page=per_page) # 创建分页器 pagination = Pagination(page=page, per_page=per_page, total=count_total_items_in_database(), css_framework='bootstrap4') # 渲染模板 return render_template('index.html', data=data, pagination=pagination) ``` 在上面的示例中,我们首先使用`get_page_args`函数从请求参数中获取当前页码和每页显示的数量。然后,我们从数据库中获取数据,并使用`Pagination`类创建一个分页器对象。最后,我们将数据和分页器对象传递给模板进行渲染。 在模板中,你可以使用`prev_href`,`next_href`和`links`属性来生成分页器的HTML代码。以下是一个简单的示例: ```html <div class="pagination"> <a href="{{ pagination.prev_href() }}">Previous</a> {% for page in pagination.links %} {% if page == '...' %} <span class="ellipsis">...</span> {% elif page == pagination.page %} <span class="current">{{ page }}</span> {% else %} <a href="{{ page }}">{{ page }}</a> {% endif %} {% endfor %} <a href="{{ pagination.next_href() }}">Next</a> </div> ``` 上面的代码会生成一个类似于以下HTML代码的分页器: ```html <div class="pagination"> <a href="/?page=1&per_page=10">Previous</a> <a href="/?page=1&per_page=10">1</a> <a href="/?page=2&per_page=10">2</a> <a href="/?page=3&per_page=10">3</a> <a href="/?page=4&per_page=10">4</a> <a href="/?page=5&per_page=10">5</a> <span class="ellipsis">...</span> <a href="/?page=10&per_page=10">10</a> <a href="/?page=2&per_page=10">Next</a> </div> ``` 此外,你还可以使用`prev_disabled`和`next_disabled`属性来禁用“上一页”和“下一页”链接,如果当前页码是第一页或最后一页的话。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chuntian_tester

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值