flask 学习(二)

来源:互联网 发布:淘宝店铺三个月未经营 编辑:程序博客网 时间:2024/05/17 05:10

1.模板继承

为了使资源的有效利用,共享页面的公共部分

templates 下base.html

<html>
    <head>
    {% if title %}
    <title>{{title}}-blog</title>
    {% else %}
    <title>blog</title>
    {% endif %}
    </head>
    <body>
    <div>blog:<a href="/index">Home</a></div>
    <hr>
    {% with messages = get_flashed_messages() %}
    {% if messages %}
    <ul>
    {% for message in messages %}
        <li>{{ message }}</li>
    {% endfor %}
    </ul>
    {% endif %}
    {% endwith %}
    {% block content %}{% endblock %}
    </body>
</html>

index.html

{% extends "base.html" %}
{% block contend %}
<h1>Hi,{{user.nickname}}!</h1>
{% for post in posts %}
<div><p>{{post.author.nickname}} says: <b>{{ post.body }}</b></p></div>
{% endfor %}
{% endblock %}

base.html 是父页面,也就是公共部分

  {% block content %}

子页面的内容呈现

{% endblock %}


{% extends "base.html" %}




0 0
原创粉丝点击