【Flask】 python学习第一章 - 5.0 模板
jinjia2 模板 python实现 flask 内置语言 参照Djago实现
设置模板文件夹
设置模板语言 jinja2
demo6_template.html ----> 从代码渲染值
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 你有事情我帮忙 我住隔壁我姓王<br> {#取值#} {{ my_int}} {{ my_list}} </body> </html>
demo6_template.py ------> render_templete("demo6_template.html", 传入渲染值 )
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from flask import Flask, request, render_template app = Flask(__name__) # 展示模板渲染 @app.route("/") def demo1(): my_str = "12345" my_int = 10 my_list = ["a", "b", "c", "d"] return render_template('demo6_template.html', my_str=my_str, my_int=my_int, my_list=my_list) if __name__ == '__main__': app.run(debug=True,port=2222)
过滤器
{{itheima | upper}}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from flask import Flask, request, render_template app = Flask(__name__) # 展示模板渲染 @app.route("/") def demo1(): my_str = "12345" my_int = 10 my_list = ["a", "b", "c", "d"] my_dict = [ {"username":"白菜", "price": 20 }, { "username": "萝卜", "price": 30 } ] return render_template('demo6_template.html', my_str=my_str, my_int=my_int, my_list=my_list, my_dict=my_dict ) # 自定义过滤器 本质上python函数 # 装饰器 反转 # 过滤器名称 # 方式一 @app.template_filter("listreverse") def do_listreverse(li): temp = list(li) temp.reverse() return temp # 方式二 直接添加过滤器 # app.add_template_filter(do_listreverse,"listreverse") if __name__ == '__main__': app.run(debug=True,port=2222)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 你有事情我帮忙 我住隔壁我姓王<br> {#取值#} {{ my_int}} {{ my_list}} {#大写#} {{ 'itheima' | upper}} {#小写#} {{ 'ITHEIMA' | lower }} {#反转#} {{ 'THEIMA' | reverse }} {#反转小写#} {{ 'ITHEIMA' | reverse | lower }} {#默认html字符串按照原样子显示 如果标识safe 就会按照html语言格式 进行执行 相反 escape#} {{ my_list | safe }} {#传参数 求和 #} {{ my_dict | sum(attribute='price') }} {#列表反转#} <br> {{ my_list | listreverse }} <br> {{ my_list }} </body> </html>
循环遍历
{% for item in my_list if my_list.id != 5 %}
{% if loop.index == 1%}
<li>item.value<li>
{% endif %}
{% endfor %}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
未完待续
代码复用
macro 宏
# 定义所有宏文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form> <label>用户名:</label><input type="text" name="username"><br/> <label>身份证号:</label><input type="text" name="idcard"><br/> <label>密码:</label><input type="password" name="password"><br/> <label>确认密码:</label><input type="password" name="password2"><br/> <input type="submit" value="注册"> </form> {% macro input(label='',type='',value = '',name='') %} <label>{{ label }}</label><input type = "{{ type }}" name = "{{ name }}"> {% endmacro %} <form> {{ input('用户名',name = 'username') }} </form> </body> </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def index(): return render_template("demo1_template.html") if __name__ == '__main__': app.run()
继承
demo:
{%block contentblock%}
填充内容
{%endblock%}
extends:
{% extends "demo.html"%}
{%block contentblock%}
{{super()}} 继承父类内容
子类内容
{%endblock%}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def index(): return render_template("demo1_template.html") @app.route("/demo1") def demo1(): return render_template("demo2_template.html") @app.route("/demo") def demo(): return render_template("demo.html") if __name__ == '__main__': app.run()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
{% extends 'demo2_template.html'%} {% block contentblock %} {{ super() }}<br/> 我是子类中间<br/> {% endblock %}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> 我的顶部<br/> {% block contentblock %} 我是父类的中间<br/> {% endblock %} 我是底部<br/> </body> </html>
包含:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from flask import Flask, render_template app = Flask(__name__) @app.route("/") def index(): return render_template("demo1_template.html") # 宏 @app.route("/demo1") def demo1(): return render_template("demo2_template.html") # 继承 @app.route("/demo") def demo(): return render_template("demo.html") # 包含 @app.route("/demo2") def demo2(): return render_template("demo3_template.html") if __name__ == '__main__': app.run()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<h1>哈哈哈哈哈哈</h1>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% include "include.html" %} {% include "include.html" %} {% include "include.html" %} {% include "include.html" %} </body> </html>
未完待续