使用flask渲染模板
- jinja2 模板引擎参考django
- 功能强,速度快
- 沙箱执行,安全
在templates 文件夹下新建index.html文件
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"> <!--设置网页编码--><title>这是首页</title> <!--设置网页标题 --><h1> 这是首页文字</h1> <!--h1 标题--></head><body></body></htm
在templates 文件夹下创建user.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户中心</title>
<h1>welcome {{ name }}</h1>
</head>
<body>
</body>
</ht
app.py 文件代码
from flask import Flask,render_template
app=Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/user/<name>')
def user(name):
return render_template('user.html',name=name)
if __name__ == '__main__':
app.run(debug=True)
模板传参
render_template() 函数的第一个参数是指定模板文件的名称, 比如这里的 user.html ,第二个参数为可选项可以为空,或者用来传递变量
模板接收变量,要把变量放在{{}} 中 模板中要写注释 格式为{{ # #}}
@app.route('/')
def index():
tittle="python key and value"
author='tom_jack'
return render_template('index.html',var1=tittle,var2=author)

视图中 如果有多个变量要传递给模板 ,可以使用**locals()方法 ,只要模板变量名和本地变量名 相同即可
主要**locals() 会传递所有本地变量
app=Flask(__name__)
@app.route('/')
def index():
tittle="python key and value"
author='tom_jack'
return render_template('index.html',**locals())
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!--设置网页编码-->
<title>这是首页</title> <!--设置网页标题 -->
<h1> 这是首页文字</h1> <!--h1 标题-->
</head>
<body>
{{ tittle }}<br>
{{ author }}
</body>
</html>
if使用
jinja2 模板中也可以使用if 和 for 循环控制语句, 控制模板渲染方向。if 和 for 语句应该放到{% %} 中
{% if %} <!-- 满足条件时候-->
{% else %}
{% endif %}
html 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!--设置网页编码-->
<title>这是首页</title> <!--设置网页标题 -->
<h1> 这是首页文字</h1> <!--h1 标题-->
</head>
<body>
{% if name %} <!--name 值存在会输出-->
<h1>产生的随机数有效</h1>
{% else %} <!-- name 值不存在,则输出产生随机数 -->
<h1>产生随机数无效</h1>
{% endif %} <!-- 结束 -->
</body>
</html>
from flask import Flask,render_template
from random import randint
app=Flask(__name__)
@app.route('/')
def index():
tittle="python key and value"
author='tom_jack'
rande1= randint(0,1)
return render_template('index.html',name=rande1)
from random import randint
app=Flask(__name__)
@app.route('/')
def index():
tittle="python key and value"
author='tom_jack'
rande1= randint(1,3)
return render_template('index.html',name=rande1)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!--设置网页编码-->
<title>这是首页</title> <!--设置网页标题 -->
<h1> 这是首页文字</h1> <!--h1 标题-->
</head>
<body>
{% if name ==1 %} <!--name 值存在会输出-->
<h1>1等奖</h1>
{% elif name == 2 %}
<h2>二等奖</h2>
{% elif name == 3 %}
<h1> 3等奖</h1>
{% endif %}
</body>
</html>
for 使用
for 语句可以遍历有序对象 ,比如字符串,列表,元组等等
for 目标 in 对象:
循环体
str = "www.google.com"
for i in str:
print(i)
模板中可以这样用
{% for 目标 in 对象%}
<p> 目标 </p>
{% endfor %}
jinja2 中 for 循环内置常量:
- loop.index : :当前迭代索引 (从1 开始)
- loop.index0 :当前迭代索引 (从 0 开始)
- loop.first
- loop.last :是否是最后一次迭代 ,是的话返回 true 不是为 False
loop.length : 返回序列长度
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!--设置网页编码--> <title>这是首页</title> <!--设置网页标题 --> <h1> 这是首页文字</h1> <!--h1 标题--> </head> <body> <table> <thead> <th> 商品名称</th> <th> 商品价格</th> </thead> <tbody> <meta charset="UTF-8"> {% for good in goods %} <tr> <td>{{ good.name }}</td> <td>{{ good.price }}</td> </tr> {% endfor %} </tbody> </table> </body> </html>app=Flask(__name__) @app.route('/') def index(): goods=[ {'name':'衣服','price':100}, {'name':'jacket','price':20}, {'name':'西服','price':300}, {'name':'短裙','price':500} ] return render_template('shop.html',**locals())
