Flask页面模板化代码片段

文中展示了Flask官网的Tutorial示例中用到的模板化代码片段,以备查阅。

 

base.html:

{% block title %}{% endblock %}

{% if g.user %}
{% else %}
{% endif %}

{{ g.user['username'] }}

{{ url_for('static', filename='style.css') }}
{{ url_for('auth.logout') }}
{{ url_for('auth.register') }}
{{ url_for('auth.login') }}

{% for message in get_flashed_messages() %}
{% endfor %}

*.html:

{% extends 'base.html' %}

{% block header %}
    {% block title %}Log In{% endblock %}
{% endblock %}

{{ request.form['body'] }}

{% if g.user %}
{% endif %}

{% for post in posts %}
    {{ post['title'] }}
    {{ post['username'] }}
    {{ post['created'].strftime('%Y-%m-%d') }}
    
    {% if g.user['id'] == post['author_id'] %}
        {{ url_for('blog.update', id=post['id']) }}
    {% endif %}
    
    {{ post['body'] }}
    
    {% if not loop.last %}
    {% endif %}
{% endfor %}

{{ request.form['title'] or post['title'] }}

{{ request.form['body'] or post['body'] }}

{{ url_for('blog.delete', id=post['id']) }}

 

posted @ 2018-08-15 08:20  快乐的凡人721  阅读(204)  评论(0编辑  收藏  举报