当前位置:网站首页>Flask模板
Flask模板
2022-07-18 01:38:00 【出其东门225】

漂亮的页面需要 html 、 css 、 js .
可以把这一堆字段串全都写到视图中, 作为 HttpResponse() 的参数,响应给客户
问题
视图部分代码臃肿, 耦合度高
这样定义的字符串是不会出任何效果和错误的
效果无法及时查看.有错也不容易及时发现
在 Flask中,配套的模板是 Jinja2,Jinja2的作者也是Flask的作者。 这个模板非常的强大,并且执行效率高。
步骤
创建模板
在 应用 同级目录下创建模板文件夹 templates . 文件夹名称固定写法
在 templates 文件夹下, 创建 应用 同名文件夹. 例, Book
在 应用 同名文件夹下创建 网页模板 文件. 例 : index.html 设置模板查找路径 模板处理数据
设置模板查找路径
模板处理数据
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/test')
def index():
return render_template('index.html')模板传参
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
return
render_template('index.html',uname='sxt')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SXT</title>
</head>
<body>
从模版中渲染的数据
<br>
{
{ uname}}
</body>
</html>
@app.route('/')
def hello_world():
context = {
'uname': 'momo',
'age': 18,
'country': 'china',
'childrens': {
'name': 'mjz',
'height': '62cm'
}
}
return
render_template('index.html',
**context)模板使用url_for函数
@app.route('/accounts/login/<name>/')
def login(name):
print(name)
return '通过URL_FOR定位过来的!!!'<a href="{
{
url_for('login',p1='abc',p2='ddd',name='江理') }}">登录</a>过滤器介绍
有时候我们想要在模版中对一些变量进行处理,那么就必须需要类 似于Python中的函数一样,可以将这个值传到函数中,然后做一些 操作。
在模版中,过滤器相当于是一个函数,把当前的变量传入到过滤器 中,然后过滤器根据自己的功能,再返回相应的值,之后再将结果 渲染到页面中
@app.route('/')
def hello_world():
return
render_template('index.html',postion=-1)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SXT</title>
</head>
<body>
<h3>过滤器的基本使用</h3>
<p>位置的绝对值为[未使用过滤器]:{
{ postion}}
</p>
<p>位置的绝对值为[使用过滤器]:{
{
postion|abs}}</p>
</body>
</html>
Jinja模板自带过滤器



defalut过滤器
<body>
<h1>default过滤器</h1>
过滤前的昵称数据是:{
{nick_name}}<br>
过滤后的昵称数据是:{
{nick_name | default('用
户1',boolean=true)}}<br>
过滤后的昵称数据是:{
{nick_name or '用户2'}}
<br>
</body>其它过滤器
自定义过滤器
只有当系统提供的过滤器不符合需求后,才须自定义过滤器
过滤器本质上就是一个函数。
如果在模版中调用这个过滤器,那么就会将这个变量的值作为第一 个参数传给过滤器这个函数, 然后函数的返回值会作为这个过滤器的返回值。
需要使用到一个装饰器: @app.template_filter('过滤器名称')
流程控制-选择结构
所有的控制语句都是放在 {% ... %} 中,并且有一个语句 {% endxxx %} 来进 行结束!
if:if语句和python中的类似,可以使用 >,=,==,!= 来进行判 断,也可以通过 and,or,not,() 来进行逻辑合并操作
<h1>选择结构</h1>
{% if uname == 'sxt' %}
<p>whuim</p>
{% endif %}
{% if uname == 'whuim'%}
<p>江理</p>
{% else %}
<p>江西理工</p>
{% endif %}
{% if uname == 'whuim' %}
<p>王慧明</p>流程控制-循环结构
for...in... for循环可以遍历任何一个序列包括列表、字典、元组。并且 可以进行反向遍历,以下将用几个例子进行解释:
<ul>
{% for user in users%}
<li>{
{ user}}</li>
{% endfor %}
</ul>
边栏推荐
- Financial banking software testing super large strategy, the most popular financial banking big secret cover questions
- MySQL - default value constraint for table fields
- VOS客户端上服务器和软交换状态显示红灯的原因分析
- CH549/CH548學習筆記9 - USB Device端點處理過程
- 野指针问题: 复习橙其技术
- UGUI源码解析——ClipperRegistry
- Openresty Lua resty lrucache cache
- PwnTheBox,Web:Double-S
- Ch549/ch548 learning notes 3 - UART
- 关于QProcess的使用问题解释
猜你喜欢

Mathematical modeling -- the road from nature to Rationality -- Introduction to mathematical modeling (learning note 1)

JVM personal learning notes

PwnTheBox,Web:Double-S

The unique index exception of the dream database in the Xinchuang environment cannot intercept the duplicatekeyexception

Ch549/ch548 learning notes 3 - UART

2022/07 CSI tool installation process record

MySQL - unique key constraint for table fields

uniapp授权登陆获取用户信息和code

Liquibase learning - problem solving: start error reporting

Financial banking software testing super large strategy, the most popular financial banking big secret cover questions
随机推荐
What is the difference between the then second parameter in promise and catch?
同花顺软件炒股线上开户可以免费吗?开户安全吗?
[MCU simulation project] the nixie tube displays 0 to 5 in turn (Proteus schematic +keil code)
Is online fund account opening safe? Novice asks for guidance
Stm32+a4988 control stepper motor
野指针问题: 复习橙其技术
Ch549/ch548 learning notes 9 - USB device endpoint processing
数据处理Numpy的基本使用方法
Summarize the strange interaction between reader and live broadcast: review ShuangDi Technology
Ugui source code analysis - iclipper
Leetcode 300: longest increasing subsequence
Liquibase learning 3 - loglevel log level, common commands
LeetCode 565 数组嵌套[dfs 记忆化] HERODING的LeetCode之路
JVM性能调优方式
Cause analysis of the red light of the server and soft switch status on the Vos client
CH549/CH548學習筆記9 - USB Device端點處理過程
同花顺软件炒股线上开户收费吗?开户安全吗?
Solidworks装配体常见问题汇总(随时更新)
无心剑中译狄兰·托马斯《不要温顺地走进那个良夜》
线上基金开户是否安全呢?在线求答案