1. 模板
- 在项目下创建 templates 文件夹,用于存放所有的模板文件
创建视图函数,进行模板渲染
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return 'index'
@app.route('/demo1')
def demo1():
my_int = 10
my_str = "哈哈"
my_list = [1, 4, 7, 9 ,6]
my_dict = {
"id": 1,
"name": "laowang"
}
return render_template('template.html',
my_int=my_int,
my_str=my_str,
my_list=my_list,
my_dict=my_dict
)
if __name__ == '__main__':
app.run(debug=True)
html中模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{
{
my_str }}<br/>
{
{
my_int }}<br/>
{
{
my_list }}<br/>
{
{
my_dict }}<br/>
取值<br/>
{
{
my_dict['name'] }}<br/>
{
{
my_dict.id }}<br/>
{
{
my_list[0] }}<br/>
{
{
my_list.0 }}<br/>
计算<br/>
{
{
my_int + my_list.1 }}<br/>
</body>
</html>
运行效果
2. 过滤器
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return 'index'
@app.route('/demo1')
def demo1():
my_int = 10
my_str = "<h1>我是大标题</h1>"
my_list = [1, 4, 7, 9, 6]
my_dict = {
"id"