Simple tab:
Label: use {%%} tags in HTML
Common system tags: load, for, if, with, csrf_token, verbatim,
Block endblock \ extends \ comment endcomment \ cycle (silent) \ custom label
1,with:
Assign values to variables in html
{% with name=obj.group.name%} #For long variable assignment {{name}} #Call IP directly to display the value of host.name.ip
2,csrf_token:
Before using the form form, you will be prompted for Forbidden:CSRF verification failed. Request aborted.
Solution 1. Comment out the middleware CSRF in settings
Solution 2: add CSRF token tag in form form form
<form action="checkuser/" method="post"> <input type="text" name="username" placeholder="User name" /> <input type="text" name="password" placeholder="Password" /> <input type="submit" value="Deng Six" /> {% csrf_token %} {#Using this sentence, there is no need to comment out the CSRF#} </form>
3,verbatim
Unmark the rendering template to display the contents of verbatim in text
{% verbatim %} {{ for i in [1,2,3,4] }} {{ i }} {{ endfor }} {% endverbatim %} //Web page display results: {{ for i in [1,2,3,4] }} {{ i }} {{ endfor }}
4. autoescape, recognize or not recognize strings containing html tags. By default, html tags are not recognized. It has the same function as the filter safe, django's mark UU safe.
Parameters: on, off, default on
html_str = "<a href='f-t'>Point me</a>" {% autoescape off %} {{ html_str }} {% endautoescape %} #Results: //Point me {% autoescape off %} {{ html_str }} {% endautoescape %} #Results: <a href='f-t'>Point me</a>
5. Comment comment
{% comment %} This section is not displayed on the web page {% endcomment %}
Cycle 6, cycle iteration, repeat from 1 to 4, when the reset cycle is encountered, start from 1 again, silent uses the previous value
{% cycle '1' '2' '3' '4' as num%} {% cycle num %} {% cycle num silent %} #Repeat output 2 {% resetcycle %} {% cycle num %} {% cycle num %} {% cycle num %} {% cycle num %} #Application scenario: interlaced color change {% for i in list%} <tr class={% cycle 'blue' 'red' %}>lala</tr> {%endfor%}
7. debug input debugging information
{% debug %}
The first parameter that is not empty or false in ‚ 8 ‚ firstof ‚ input parameter
{% firstof da 'e' 'f' %} #Because da is an undefined variable name, result: 'e'
9. User defined simple tag:
3 fixed principle: the new package name in APP is fixed: templatetags
Library instantiation name is fixed in views: register=template.Library()
User defined function, decorated with register.simple-tag
Use: python file%} defined by {% load above HTML
#Create a new python file under templatetags, custom tab.py #Import template from django import template #Instantiation register = template.Library() #register is fixed and cannot be changed to another name @register.simple_tag #Decoration daxie def daxie(str1): #Make the string uppercase. str1 = str(str1).upper() return str1
#Load the custom python file above the HTML {% load custom_tag %} {% daxie 'WERWERFSEFkjdDFGSdfeflnnidhggindSDGSEGSFVBNHR' %} #The parameter is written directly after the simple tab name. There can be multiple parameters. #Result# WERWERFSEFKJDDFGSDFEFLNNIDHGGINDSDGSEGSFVBNHR
filter:
Chain operation is supported. Each result is passed to the next filter in turn.
Filter call format {{value | filter name 1: parameter 1 | filter name 2: parameter 2 |...}}
Description:
Value: only one value can be passed
Pipe character: left and right spaces are not allowed.
Filter name: function name
Left and right spaces are not allowed.
Parameter: only one parameter can be passed. Multiple values can be quoted in the dictionary or list, "{'name':'david','age':19}"
1. There are 30 built-in filters: add\default\length\filesizeformat\slice\date\safe\truncatechars\truncatewords\cut\join\add\urlencode
#Add before add {{ 30|add:' 1'|add:' 3'|add:' 2'|add:' 4'|add:' 5' }} #Multiple | pass the previous value each time #Result: sq 1 3 2 4 5 #urlencode remember javascript address translation, it's all the same {{ "http://127.0.0.1:8000/a/test/666"|urlencode }} Result: http%3A//127.0.0.1%3A8000/a/test/666 <br> #join joins a list or character using the specified character #View passed in {'ls': [122111555]} {{ ls|join:'@' }} Result:122@111@555 {{ '1 2232'|join:'-' }} Results: 1- - - - -2-2-3-2 <br> #cut removes the specified string from the string {{ '1112312312311221313'|cut:'23' }} Results: 1111111221313 <br> #truncatechars removed from nth character {{ '1234567890'|truncatechars:2 }} Results: 1... <br> #truncatewords calculate the number of words with spaces, and keep n words {{ '123 456 78 90'|truncatewords:2 }} Results: 123 456 ... <br> #The value of the safe variable is safe, so that the HTML string is escaped, and link = "< p > < a href ='http://127.0.0.1:8000 / A / test / 666 '> hyperlink < / a > < p > {{ link }} {#Result:<p><a href='http://127.0.0.1:8000 / A / test / 666 '> hyperlink {{ link|safe }} Results: hyperlinks <br> #Format date. The passed in is datetime.datetime.now() {{ dt|date:"Y-m-d H:i:s" }} #Results: October 11, 2019 14:58:16 {{ dt|date }} #Results: Oct. 11, 2019 <br> #Slice slice: {{ '0123456'|slice:"0:3" }} #Result: 0123 <br> #- filesizeformat to convert values to computer units {{ 1000|filesizeformat }} # 1000 bytes {{ 10000|filesizeformat }} # 9.8 KB {{ 1000000000|filesizeformat }} # 953.7 MB <br> #Default if the previous variable is empty or does not exist, the default value will be displayed and the sname value will not be changed. {{ sname|default:'none' }} #Result: none <br> #Length return length {{ 'abc'|length }}
2. Custom filter:
The definition method of 「 is the same as that of simple 」 tag, and the calling method is different.
Fixed principle: the new package name in APP is fixed: templatetags
Library instantiation name is fixed in views: register=template.Library()
Fixed calling format in HTML
Custom function, decorated with register.filter
You can only use two values, usually one is before the decoration and the other is the decoration parameter.
Use: python file%} defined by {% load above HTML
Call: call using {}}, format: {{str|filtername:args}}}}
#Create a new python file under templatetags, custom filter.py from django import template #Import template module register = template.Library() #Instantiation, name must be register @register.filter def filter_ellipsis(str1, args1): #- up to two parameters #Intercept the length of args1 from str1 and add... To return to the front end if len(str1) > args1: str1 = str1[:args1] + r"..." return str1
Load custom python files above HTML
{% load custom_tag %} {{ "01234567890123456789012345678901234567890123456789"|filter_ellipsis:20 }} #Results: 01234567890123456789...
The difference between Filter and simple tab:
'1. HTML tags are different. filter uses {}}, tag uses {%%}
filter can be used for condition judgment in if/for, tag can't
3. The filter can pass at most two parameters, and the tag can be multiple.