Template syntax of filters and labels
filter:
Somewhat similar to a small filter method.
Features: will | The first argument of the left as a filter when the | current filter to the right of the second parameter
Filter (|)
(front-end code does not have to write you can write the backend passed to the front page at the front)
front and rear end unescaping
the front
| safe
back-end
views
def login(request):
n = 123
f = 12.12
s = '你妹的 真难'
l = [1,2,3,4,5,6]
d = {'username':'jason','password':[123,222,444]}
t = (1,2,3,4,5)
se = {1,2,3,4}
b = True
ctime = datetime.now()
file_size = 342384234234
w = '奥术 大师件 大事肯德 基按 实际对 拉 螺栓空当接 龙'
w1 = 'asdash ashd jkh sadas asdjjs had sjklad j a kjkjdsklas dkjsakldj dlsjakld '
w2 = 'he llow ord wqe asdsad sadsadsa dsadasds adasds adsadsadsada sdsad'
obj = MyClass()
sss = "<h1>下午上课 一脸懵逼</h1>"
sss1 = "<script>alert(123)</script>"
sss2 = "<a href='http://www.xiaohuar.com'>下午上课 需要激情</a>"
res = mark_safe(sss2)
xo = '123213213'
xo1 = 222
yyy = {'user_list':[1,'22',{'username':['jason','egon']}]}
return render(request,'login.html',locals())
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>{{ file_size|filesizeformat }}</p> <!--会自动帮你转化-->
<p>截取10个字符 三个点也算{{ w1|truncatechars:10 }}</p>
<p>截取10个字符 三个点也算{{ w|truncatechars:10 }}</p>
<p>安装空格截取单词 三个点不算{{ w1|truncatewords:6 }}</p>
<p>安装空格截取单词 三个点不算{{ w|truncatewords:6 }}</p>
<p>安装空格截取单词 三个点不算{{ w2|truncatewords:6 }}</p>
<p>{{ l|slice:'0:5' }}</p> <!--切片 -->
<p>{{ l|slice:'0:5:2' }}</p>
<p>{{ ctime|date:'Y-m-d' }}</p> <!--日期-->
<p>{{ ctime|date:'Y年/m月' }}</p>
<p>{{ sss|safe }}</p> <!--使得后端传来的<h1>表现不会变成字符串-->
<p>{{ sss1|safe }}</p>
<p>{{ res }}</p>
<p>{{ xo|default:'' }} <!--有值就拿值 没值就用后面默认的-->
</body>
</html>
result:
318.9 GB
截取10个字符 三个点也算asdash ...
截取10个字符 三个点也算奥术 大师件 ...
安装空格截取单词 三个点不算asdash ashd jkh sadas asdjjs had ...
安装空格截取单词 三个点不算奥术 大师件 大事肯德 基按 实际对 拉 ...
安装空格截取单词 三个点不算he llow ord wqe asdsad sadsadsa ...
[1, 2, 3, 4, 5]
[1, 3, 5]
2019-10-23
2019年/10月
下午上课 一脸懵逼(其实很大,只是这里显示不出来,他是<h1>标签)
下午上课 需要激情
123213213
Template grammar symbols on two kinds of
relevant variables {} {}
{} Logical associated %%
label
html
{% for foo in l %}
<p>{{ forloop }}</p>
{% endfor %}
This forloop it comes inside
Print out the contents of such
{'parentloop': {}, 'counter0': 0, 'counter': 1, 'revcounter': 6, 'revcounter0': 5, 'first': True, 'last': False}
{'parentloop': {}, 'counter0': 1, 'counter': 2, 'revcounter': 5, 'revcounter0': 4, 'first': False, 'last': False}
{'parentloop': {}, 'counter0': 2, 'counter': 3, 'revcounter': 4, 'revcounter0': 3, 'first': False, 'last': False}
{'parentloop': {}, 'counter0': 3, 'counter': 4, 'revcounter': 3, 'revcounter0': 2, 'first': False, 'last': False}
{'parentloop': {}, 'counter0': 4, 'counter': 5, 'revcounter': 2, 'revcounter0': 1, 'first': False, 'last': False}
{'parentloop': {}, 'counter0': 5, 'counter': 6, 'revcounter': 1, 'revcounter0': 0, 'first': False, 'last': True}
You can see the value of counter0 and counter1 growing, starting with 0 and 1 until 5 and 6, first only the first cycle when the guess is True, last and only time in the last cycle will be True.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% for foo in l %}
<p>{{ forloop }}</p>
{% if forloop.first %}
<p>这是我的第一次</p>
{% elif forloop.last %}
<p>这是最后一次了啊</p>
{% else %}
<p>嗨起来 大宝贝~</p>
{% endif %}
{% endfor %}
{% for foo in xo %}
<p>{{ forloop.counter }}:{{ foo }}</p> <!--这里的‘:’只是一个普通的字符-->
{% empty %} <!--在xo是空的时候才会走,才能走下面那一句-->
<p>你给我的对象是个空的没法进行for循环</p>
{% endfor %}
{% for foo in d.items %}
<p>{{ foo }}</p>
{% endfor %}
{% for foo in d.keys %}
<p>{{ foo }}</p>
{% endfor %}
{% for foo in d.values %}
<p>{{ foo }}</p>
{% endfor %}
</body>
</html>
result:
这是我的第一次
嗨起来 大宝贝~
嗨起来 大宝贝~
嗨起来 大宝贝~
嗨起来 大宝贝~
这是最后一次了啊
1:1
2:2
3:3
4:2
5:1
6:3
7:2
8:1
9:3
('username', 'jason')
('password', [123, 222, 444])
username
password
jason
[123, 222, 444]
Custom filters and labels
Step:
1 In the application name below to create a new templatetags folder (must be that name)
2 to create a new name in any change folder py file
3 fixed to write two lines of code in the py file
from django.template import Library
register = Library()
Then py files in your new templatetags file can create a custom filter
such as:
from django.template import Library
register = Library()
@register.filter(name='myplus')
def index(a,b):
return a+b
Then you can call up at the front, called by
{% load my_tag %} #固定写法,你新建的py文件叫什么,这里就写什么
{{ 123|myplus:123}}
result:
246
Similarly, a custom label is written in the same position
@register.simple_tag(name='mysm')
def login(a,b,c,d):
return '%s/%s/%s/%s'%(a,b,c,d)
Invocation:
{% load my_tag %}
{% mysm 1 2 3 4 %}
result:
1/2/3/4
区别 标签不能再if中使用
{% if 0|myplus:123 %} 可以用
<p>有值</p>
{% endif %}
{% if mysm 1 2 3 4 %} 不能用
<p>有值</p>
{% endif %}