奥嘉61鲁尔彻克,邪恶漫画不知火舞公园,调教日本美女小游戏
filter:过滤器,用来修改变量的显示结果
内置过滤器
default:默认值
filesizeformat:文件大小,将值格式化为可读的文件尺寸
add:给变量加参数
length:返回长度
slice:切片
first / last:取第一个/最后一个元素
join:使用字符串拼接列表
truncatechars:截断,按照字符计数
date:日期格式化
语法:{{ value|date:"y-m-d h:i:s"}}
在settings.py中配置:
use_l10n = false datetime_format = 'y-m-d h:i:s' # datetime类型 date_format = 'y-m-d' # date类型 time_format = 'h:i:s' # time类型
配置后,使用{{now}}可以实现日期格式化
safe:告诉django这段代码是安全的,不需要转义
value = "<a href='#'>点我</a>"
在app下创建一个名为templatetags的python包
在templatetags中创建py文件,文件名自定义(my_tags.py);
在py文件中写:
from django import template register = template.library() # register也不能变
写函数+装饰器
@register.filter def add_xx(value, arg): # 最多有两个 return '{}-{}'.format(value, arg) @register.filter(name='adds') # 相当于更改了函数名,使用时,使用新的函数名
在模板文件中使用,html文件
{% load my_tags %} {{ 'alex'|add_xx:'dsb' }}
注意:
符号:{% %}
for循环
<ul> {% for user in user_list %} <li>{{ user.name }}</li> {% endfor %} </ul>
variable | description |
---|---|
forloop.counter |
当前循环的索引值(从1开始) |
forloop.counter0 |
当前循环的索引值(从0开始) |
forloop.revcounter |
当前循环的倒序索引值(到1结束) |
forloop.revcounter0 |
当前循环的倒序索引值(到0结束) |
forloop.first |
当前循环是不是第一次循环(布尔值) |
forloop.last |
当前循环是不是最后一次循环(布尔值) |
forloop.parentloop |
本层循环的外层循环 |
{% for book in all_books %} <tr> ..... </tr> {% empty %} <td>没有相关的数据</td> {% endfor %}
if判断
{% if 条件1 %} xxx {% elif 条件2 %} xxx {% else %} xxxxx {% endif %}
with:给变量重命名,但只在with区域内生效
{% with hobby.2 as talk %} {# 相当于 {% with talk=hobby.2 %},其中=两边不能有空格 #} {{ talk }} {% endwith %}
csrf_token
符号:{# 要注释的内容 #}
快捷键:ctrl + ?
{# 要注释的内容 #}
母板就是一个普通的html,提取多个页面的公共部分,通过定义block块来实现
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>title</title> {% block page-css %} {% endblock %} </head> <body> <h1>这是母板的标题</h1> {% block page-main %} {% endblock %} <h1>母板底部内容</h1> {% block page-js %} {% endblock %} </body> </html>
block块:
{% block 块名 %} {% endblock %}
注意:我们通常会在母板中定义页面专用的css块和js块,方便子页面替换
子页面继承母板:{% extends '母板html' %}
{% extends 'layouts.html' %}
子页面通过重写block块,来替换母板中相应的内容
{% block page-main %} <p>世情薄</p> <p>人情恶</p> <p>雨送黄昏花易落</p> {% endblock %}
组件:一小段html代码段
可以将常用的页面内容如导航条,页尾信息等组件保存在单独的文件中,然后在需要使用的地方导入
{% include 'navbar.html' %}
目的:更改setting中静态文件的别名时,不影响在更改之前的静态文件的引用,即引用会跟随别名的更改而自动更改,这样就不会报错了
方法一:使用static,原本路径:/static/images/hi.jpg
{% load static %} <img src="{% static "images/hi.jpg" %}" alt="hi">
{% load static %} {% static "images/hi.jpg" as myphoto %} <img src="{{ myphoto }}">
方法二:使用get_static_prefix,原本路径:/static/images/hi.jpg
{% load static %} <img src="{% get_static_prefix %}images/hi.jpg" alt="hi"> {# 补充:获取别名 #} {% get_static_prefix %}
{% load static %} {% get_static_prefix as static_prefix %} <img src="{{ static_prefix }}images/hi.jpg" alt="hi">
和自定义filter类似,区别:接收的参数更灵活,能接收万能参数
定义注册simple_tag
@register.simple_tag def join_str(*args, **kwargs): return '{} - {} '.format('*'.join(args), '$'.join(kwargs.values())) @register.simple_tag(name='join') # 相当于更改了函数名,使用时,使用新的函数名
使用自定义simple_tag
{% load my_tags %} {% join_str '1' '2' k1='3' k2='4' %}
在app下的templatetags(python包)中创建py文件,文件名自定义(my_inclusion.py);
在py文件中写:
from django import template register = template.library() # register也不能变
写函数+装饰器
@register.inclusion_tag('result.html') # result.html 是内容的html def show_results(n): n = 1 if n < 1 else int(n) data = ["第{}项".format(i) for i in range(1, n+1)] return {"data": data}
在result.html中写:
<ul> {% for choice in data %} <li>{{ choice }}</li> {% endfor %} </ul>
在模板文件中使用
{% load my_inclusion %} {% show_results 10 %}
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论