您当前的位置: 首页 >  flask

Flask(3):Flask的模板templates

发布时间:2020-08-03 21:04:02 ,浏览量:8

文章目录
  • 1.前端使用后端的值
  • 2.后端给前端传入标签
  • 3.for和if用法
  • 4.模板继承
1.前端使用后端的值

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) 

在这里插入图片描述

3.for和if用法

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}}            
关注
打赏
1688896170
查看更多评论

暂无认证

  • 8浏览

    0关注

    105695博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0465s