flask学习笔记-模板和条件语句

继续使用上一节的模板,模板如下:

microblog\
  venv\
  app\
    __init__.py
    routes.py
  microblog.py

本节主要是记录,模板的使用和条件语句,循环的的使用


1. 模板的使用

在使用模板之前需要创建一个存储模板的目录,在app的目录下创建一个templates文件夹,在文件app/templates/index.html中写入以下代码

index.html

<html>
    <head>
        <title>{{ title }} - Microblog</title>
    </head>
    <body>
        <h1>Hello, {{ user.username }}!</h1>
    </body>
</html>

routes.py

from flask import render_template
from app import app

@app.route('/')
@app.route('/index')
def index():
    user = {'username': 'Miguel'}
    return render_template('index.html', title='Home', user=user)

在routes.py中用到了,render_template() 函数,作用是把函数中含有的参数替换成模板中的{{…}}

也就是在渲染的过程中用实际值来替换占位符

2. if语句

模板也支持在{%…%}块内使用控制语句,模板index.html的下一个版本添加一条if语句

<html>
    <head>
        {% if title %}
        <title>{{ title }} - Microblog</title>
        {% else %}
        <title>Welcome to Microblog!</title>
        {% endif %}
    </head>
    <body>
        <h1>Hello, {{ user.username }}!</h1>
    </body>
</html>

if语句的使用

{% if something %}
...
{% else %}
...
{% endif %}

在if语句中,必须有endif来进行结束,else可以没有

3. for循环

for语句的使用

{% for item in items %}
    #输出或使用item,做一些操作
{% endfor %}

routes.py中视图函数index更新

from flask import render_template
from app import app

@app.route('/')
@app.route('/index')
def index():
    user = {'username': 'Miguel'}
    posts = [
        {
            'author': {'username': 'John'},
            'body': 'Beautiful day in Portland!'
        },
        {
            'author': {'username': 'Susan'},
            'body': 'The Avengers movie was so cool!'
        }
    ]
    return render_template('index.html', title='Home', user=user, posts=posts)

index.html中使用for循环来更新posts中的值

<html>
    <head>
        {% if title %}
        <title>{{ title }} - Microblog</title>
        {% else %}
        <title>Welcome to Microblog</title>
        {% endif %}
    </head>
    <body>
        <h1>Hi, {{ user.username }}!</h1>
        {% for post in posts %}
        <div><p>{{ post.author.username }} says: <b>{{ post.body }}</b></p></div>
        {% endfor %}
    </body>
</html>

4. 模板的继承

app/templates/base.html

<html>
    <head>
      {% if title %}
      <title>{{ title }} - Microblog</title>
      {% else %}
      <title>Welcome to Microblog</title>
      {% endif %}
    </head>
    <body>
        <div>Microblog: <a href="/index">Home</a></div>
        <hr>
        {% block content %}{% endblock %}
    </body>
</html>

block控制语句定义了派生模板可以插入代码的位置。block被赋予了一个唯一的名称,派生的模板可以在提供其内容时进行引用
简化后的index.html

{% extends "base.html" %}

{% block content %}
    <h1>Hi, {{ user.username }}!</h1>
    {% for post in posts %}
    <div><p>{{ post.author.username }} says: <b>{{ post.body }}</b></p></div>
    {% endfor %}
{% endblock %}

使用extends语句建立了两个模板之间的关系,引用了base.html,block插入代码块,这样就可以不用重复的写代码了


此时的项目结构图

microblog\
  templates/
    index.html
    base.html
  venv\
  app\
    __init__.py
    routes.py
  microblog.py
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值