dw做的简单的个人网站网盘,wordpress不显示引用图片不显示,平阳做网站,徐州网站制作哪家好Flask 模板渲染
模板是包含占位符的 HTML 文件
Flask 使用 Jinja2 模板引擎来处理模板渲染。模板渲染允许你将动态内容插入到 HTML 页面中#xff0c;使得应用能够生成动态的网页内容。
创建模板#xff1a;将 HTML 文件放在 templates 文件夹中#xff0c;使用 Jinja2 占…Flask 模板渲染
模板是包含占位符的 HTML 文件
Flask 使用 Jinja2 模板引擎来处理模板渲染。模板渲染允许你将动态内容插入到 HTML 页面中使得应用能够生成动态的网页内容。
创建模板将 HTML 文件放在 templates 文件夹中使用 Jinja2 占位符。渲染模板使用 render_template 函数在视图函数中渲染模板。模板继承创建基础模板允许其他模板继承和扩展。控制结构使用条件语句和循环在模板中控制逻辑。过滤器使用过滤器格式化变量数据。宏和模板包含创建和使用宏以及模板包含提高模板的复用性。安全性Jinja2 默认对模板变量进行自动转义以防止 XSS 攻击。模板上下文将数据传递给模板并在模板中使用这些数据。 基本概念/创建模板
模板是包含占位符的 HTML 文件。
Flask 使用 Jinja2 模板引擎来渲染这些模板将 Python 数据插入到 HTML 中从而生成最终的网页。
!DOCTYPE html
html
headtitleWelcome/title
/head
bodyh1{{ title }}/h1pHello, {{ name }}!/p
/body
/htmlapp.py
from flask import Flask, render_template
app Flask(__name__)
app.route(/)
def index():return render_template(index.html,titleHome,nameMisha)if __name__ __main__:app.run(debugTrue)格式: {{ 变量名 }} 模板继承
root.html
!DOCTYPE html
html
headtitle{% block title %}My Website{% endblock %}/title
/head
bodyheaderh1My Website/h1/headermain{% block content %}{% endblock %}/mainfooterpFooter content/p/footer
/body
/htmlleaf.html
{% extends root.html %}{% block title %}Home Page{% endblock %}{% block content %}
h2Welcome to the Home Page!/h2
pContent goes here./p
{% endblock %}app.py
app.route(/leaf)
def leaf():return render_template(leaf.html)检查响应的内容确实被替换了 格式:可替换区域 {% block 变量名 %} {% endblock %}
控制结构
Jinja2 提供了多种控制结构用于在模板中实现条件逻辑和循环。
ctrl_flow.html
!DOCTYPE html
html
headtitleWelcome/title
/head
body!-- 条件语句 --{% if user %}pWelcome, {{ user }}!/p{% else %}pPlease log in./p{% endif %}!-- 循环语句 --ul{% for item in items %}li{{ item }}/li{% endfor %}/ul
/body
/htmlapp.py
app.route(/ctrl_flow)
def ctrl_flow():# return render_template(ctrl_flow.html,userZhangsan)# return render_template(ctrl_flow.html)return render_template(ctrl_flow.html,userZhangsan,items[apple,banana,orange])传了user
未传user和items
传了user和items
过滤器
过滤器用于在模板中格式化和处理变量数据。
filter.html
!DOCTYPE html
html
headtitleWelcome/title
/head
body
p{{ name|capitalize }}/p
p{{ price|round(2) }}/p
/body
/htmlapp.py
app.route(/filter)
def filter():return render_template(filter.html,namewangwu,price2.999)过滤器的写法与shell中的管道一样都是用|来表示
格式: {{ 变量名|处理方法 }} 宏和模板包含
macros.html
{% macro render_item(item) %}divh3{{ item.title }}/h3p{{ item.description }}/p/div
{% endmacro %}使用宏: macros_demo.html
{% from macros.html import render_item %}h1Items/h1
{% for item in items %}{{ render_item(item) }}
{% endfor %}app.py
app.route(/macros)
def macros():# return render_template(macros_demo.html,items[apple,banana,orange])return render_template(macros_demo.html,items[{title:apple,description:苹果},{title:banana,description:香蕉},{title:orange,description:橘子}])变量不正确时的效果 正常的显示 安全性
security.html
html
head security /head
body
p{{ user_input }}/p
/body
/htmlapp.route(/xss)
def xss():return render_template(security.html,user_inputscript alert(1) /script)自动转义Jinja2 默认会对模板中的变量进行自动转义防止 XSS 攻击。
script标签不会被认为是html元素 模板上下文
视图函数中传递的变量成为模板的上下文这些变量可以在模板中直接使用。
profile.html
h1{{ user.name }}/h1
pAge: {{ user.age }}/papp.py
app.route(/profile/username)
def profile(username):user {name: username, age: 25}return render_template(profile.html, useruser)Demo
Flask
参考
Flask模板渲染