Flask——模板的继承

先说模板的继承,定义一个父模板(命名为“father.html”),其格式为:

{% block top %}
{% endblock top %}

{% block content %}
{% endblock content %}

{% block bottom %}
{% endblock bottom %}

定义一个子模板(命名为“son.html”),其格式为:

{% extends "father.html" %}
{% block top %}
{% endblock top %}

下面举一个例子进行说明:
定义文件father.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>——————第一个按钮——————{% block top %}{% endblock top %}</h1>
    <h1>——————第二个按钮——————{% block hello %}{% endblock hello %}</h1>
</body>
</html>

定义文件son.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% extends "father.html" %}
    {% block top %}
        <br>
        <input name="第一" type="text" value=" " size="50">
    {% endblock top %}

    {% block hello %}
        <br>
        <input name="第二" type="password" value="" size="50">
    {% endblock hello %}
</body>
</html>

定义渲染的函数:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def top():
    return "这是主页"

@app.route("/hello")
def index():
    return render_template("son.html")

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

得到的效果图为:
在这里插入图片描述
以上就是Flask模板继承的基本使用方法,各位读者有疑问的可以私信博主或评论留下足迹。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值