模版中的 url_for 跟我们后台视图函数中的 url_for 使用起来基本是一模一样的。也是传递视图函数的名字 也可以传递参数。
语法格式
{{ url_for( 函数名 ,[参数1 值1 ]) }}
html
a href {{ url_for( login ,p1 abc ,p2 ddd ,name momo ) }} 登录 /a
view.py
python文件如 app.route( /accounts/login/ name / ) def login(name): print(name) return render_template( login.html )二、过滤器的使用
查看所有过滤器:http://jinja.pocoo.org/docs/dev/templates/#builtin-filters
1、过滤器的基本使用
语法格式
{{ variable|过滤器名字 }}
view.py
app.route( / ) def hello_world(): return render_template( index.html ,a -2)
html
!DOCTYPE html
html lang en
head
meta charset UTF-8
title SXT /title
/head
body
h3 过滤器的基本使用 /h3
p 位置的绝对值为[未使用过滤器] {{ a}} /p
p 位置的绝对值为[使用过滤器] {{ a|abs}} /p
/body
/html
2、default过滤器的使用
例如
view.py
app.route( / )
def hello_world():
context {
action :-1
return render_template( index.html ,**context)
html
!DOCTYPE html
html lang en
head
meta charset UTF-8
title SXT /title
/head
body
h3 过滤器的基本使用 /h3
p 个性签名[使用过滤器] {{ a|default( 此人很懒 没有任何说明 )}} /p
/body
/html
运行结果
如果
view.py
app.route( / )
def hello_world():
context {
action :-1,
a :None
return render_template( index.html ,**context)
结果
如歌要想default( )里面的值优先显示
p 个性签名[使用过滤器] {{ signature|default( 此人很懒 没有任何说明 ,boolean True)}} /p
default过滤器总结
使用方式 {{ value|default( 默认值 ) }} 。
如果value这个 key 不存在 那么就会使用 default 过滤器提供的默认值。
如果value这个 key 存在 就不会使用 default 过滤器提供的默认值。但对于value的一些特殊值 例如 None、空字符串、空列表、空字典等 想使用 default 过滤器提供的默认值。 那么就必须要传递另外一个参数 {{ value|default( 默认值 ,boolean True) }} 。
3、自定义过滤器
例子
#将模版设置为自动加载模式 app.config[ TEMPLATES_AUTO_RELOAD ] True app.template_filter( 过滤器名 ) def 过滤器名(arg1): return arg1
使用
p 使用自定义过滤器 {{内容值|过滤器名}} /p



