12.2.9 template syntax
1. MVC framework and MTV framework
-
MVC framework
M - model: Model - interact with the database, the database operation
V - view: view - shows HTML page
C - controller: Controller - scheduling role, responsible for business logic
-
MTV framework
M - model: Model - interact with the database, the database operation (ORM)
T - template: View - Templates, HTML
V - view: view - responsible for business logic
2. Variable
Variable name} {} {
{{variable name.}} # Value can be obtained by means of point
List. Index (the index value must not be negative)
dict.key / dict.keys / dict.values / dict.items
{{class examples of objects}}
obj. property / obj. method
<! - Note: ->
(.) When the system encounters a template, it will be in the following order to query:
-
Query in the dictionary (if there is a dictionary key named 'keys', query time to go first keys corresponding values)
-
Property or method
-
Digital Index
3. Filter
filters filter - to display results of modified variables
Syntax: {{value | filter_name}} / {{value | filter_name: Parameter}}
<-! ':' There is no space left no space is no space, there is at most one parameter ->
default - provide default values
{{ xxx|default:'nothing' }} # 传过来的变量不存在或者为空,使用默认值
# 注:
在setting中TEMPLATES的OPTIONS可以增加一个选项:string_if_invalid:'NOT FOUND',可以替代default的的作用。(优先级高于default)
slice -- 切片
{{ name_list|slice:'0:1' }}
filesizeformat -- 文件大小格式化
'filesize': 1*1024
{{ filesize|filesizeformat }} # 1.0KB
add -- 表示 +
{{ num|add:1 }} # 数字相加
{{ string|add:'123' }} # 字符串拼接
{{name_list|add:other_list} # 列表合并
length -- 返回变量的长度
{{ value|length }}
join 使用字符串拼接列表
{{ name_list|join:'_' }}
truncatechars -- 按照字符分割.截断的字符串,多余的用'...'表示
{{ long|truncatechars:'15' }} # 只显示15个字符,三个点也包含在15个字符内
truncatewords -- 按照单词分割(按照空格)
{{ long|truncatewords:'6' }}
date -- 日期格式化
{{ now|date:'Y-m-d H:i:s' }}
# settings配置 -- Django中就可以不用日期过滤器
USE_L10N = False
DATETIME_FORMAT = 'Y-m-d H:i:s'
safe -- 告诉django不需要转义,是安全的
{{ js|safe }} # 在Python文件中设置 from django.utils.safestring import mark_safe mark_safe('<a href="https://www.baidu.com">跳转</a>') {{ a }}
4. 自定义过滤器
-
在app01文件下创建一个templatetags的python包
-
在包内创建py文件 -- 自定义名字 ( my_ tags.py )
-
在py文件中写入
from django import template register = template.Library() # regiter不能变,固定写法
-
定义函数 + 装饰器
@register.filter() # 添加装饰器 def new_upper(value,arg=None): # arg接收过滤器的参数 return value.upper()
-
在模板中使用
{% load my_tags %} # 在模板中导入 {{ 'abc'|new_upper:dic }} # 函数名就是过滤器名
5. Tag -- 标签
-
for
{% for i in list %}
{{ forloop.counter }}
{{ i }}
{% endfor %
for循环可用的一些参数
Variable(变量) | Description(描述) |
---|---|
{{ forloop.counter }} | 当前循环的从1开始的计数 |
{{ forloop.counter0 }} | 当前循环的从0开始的计数 |
{{ forloop.revcounter }} | 当前循环的倒叙计数(到1结束) |
{{ forloop.revcounter0 }} | 当前循环的倒叙计数(到0结束) |
{{ forloop.first}} | 当前循环是否是第一次循环 布尔值 |
{{ forloop.last}} | 当前循环是否是最后一次循环 布尔值 |
{{ forloop.parentloop }} | 当前循环父级循环的forloop |
for ... empty
{% for name in name_list %}
{{ name }}
{% empty %}
空的数据
{% endfor %}
-
if
{% if p1.age < 18 %} 他还是个宝宝 {% elif p1.age == 18 %} 刚成年,可以出家 {% else %} 骚老头子坏得很 {% endif %}
<!-- 不支持算数运算 ,不支持连续判断 -->
<!-- if语句支持 and 、or、==、>、<、!=、<=、>=、in、not in、is、is not判断。 -->
-
with
{% with agoni=person_list.1.name age=person_list.1.age %}
{{ agoni }} {{ age }}
{% endwith %}
-
csrf_token
这个标签用于跨站请求伪造保护。
在页面的form表单里面写上{% csrf_token %}
form表单中有一个隐藏的input标签 name ='csrfmiddlewaretoken'
<!-- 看下图 -->
<form action="" method="post"> {% csrf_token %} <input type="text" name="name"> <button>提交</button> </form>