文章目录
1.前端使用后端的值
- 1.前端使用后端的值
- 2.后端给前端传入标签
- 3.for和if用法
- 4.模板继承
demo.py文件代码
from flask import Flask, render_template, request, redirect app = Flask(__name__, template_folder='templates') @app.route('/index/') def index(): content = { 'k1': 2, 'k2': [1, 2, 3], 'k3': {'name': '张三', 'age': 15}, 'k4':lambda x:x+1 } return render_template('index.html', **content) if __name__ == '__main__': app.run(debug=True)
index.html文件,在我们flask的模板语法中,更贴近原始python代码的取值
<ul> <h1>取单个字符:{{k1}}{k2.0}},{{k2[0]}}{k3.name}},{{k3['name']}},{{k3.get('name','空')}}{k4}}{k4(100)}}'k1':add_input} return render_template('index.html', **content) if __name__ == '__main__': app.run(debug=True)
index.html
<ul> <h1>{{k1()}}{k1()|safe}}'k1':add_input} return render_template('index.html', **content) if __name__ == '__main__': app.run(debug=True)
py文件
@app.route('/index/') def index(): content = { 'k2': [1, 2, 3], 'k3': {'name': '张三', 'age': 15}, } return render_template('index.html', **content)
html文件
<ul> <h1>遍历列表{i}}{i}}{k}}{v}}{k}}:{{v}}关注打赏