Django learning notes (5) template tags, django learning notes
About Django template tag official site https://docs.djangoproject.com/en/1.11/ref/templates/builtins/
1. IF tag
Hello World/views. py
1 from django. shortcuts import render 2 3 class Person (object): 4 def _ init _ (self, name, age, sex): 5 self. name = name 6 self. age = age 7 self. sex = sex 8 9 def say (self): 10 return "I'm % s. "% self. name11 12 def index (request): 13 # pass in a common variable 14 # input data into a variable in html: Variable 15 in views # return render(request,'index.html ', {'title': 'Welcome ', 'name': 'keinee'}) 16 17 # input dictionary variable 18 person = {'name': 'lil', 'age': 20, 'sex ': 'male'} 19 # input list variable 20 book_list = ['python', 'java ', 'C'] 21 # book_list = [] 22 # input object variable 23 # person = Person ('bucket', 18, 'female ') 24 return render(request,'index.html ', {'title': 'Welcome ', 'person': person, 'book _ list': book_list })
Views. py
(1) if... elif... else... endif
Hello World/temlplates/index.html
{% If book_list.0 = 'java' %} The first book is Java {% elif book_list.0 = 'python' %}. The first book is Python {% else % }. yes C {% endif %}
View Code
The result is: the first book is Python.
(2) if... and... or... not... endif
Note: and or can be used in the same way, but and has a higher priority than or.
Hello World/temlplates/index.html
{% If book_list or person %} has book_list or person {% endif %} {% if book_list and person %} both book_list and person have {% endif %} {% if book_list and not person %} book_list does not exist person {% endif %}
View Code
Result: Both book_list, person, book_list, and person exist. (null)
(3) if symbol operation (= ,! =,>, >=, <=, <, In, not in, is, is not)
If is XX True: if and only if XX is True, this cannot be understood for the moment.
{% If book_list.0 = 'python' %} 1. The first book is Python {% endif %} {% if book_list.0! = 'Python' %} 2. the first book is not Python {% endif %} {% if person. age <= 20%} 3. this person is not older than 20 {% else %} 4. the age of this person exceeds 20 {% endif % }{% if 'python' in book_list %} 5. python in THE book_list list {% endif %} {% if 'py' not in book_list %} 6. py in the book_list list {% endif %} {% if book_list.4 is not True %} 7. book_list.4 does not exist {% endif %} {% if book_list is not None %} 8. book_list list {% endif %}
View Code
Results: The values 1, 3, 5, 6, 7, and 8 are displayed.
2. For tag
(1) list for Loop
{% for book in book_list %}{{book}}{% endfor %}
Result: Python Java C
(2) dictionary for Loop
{% for k,v in person.items %}{{k}}:{{v}}{% endfor %}
Result: sex: male name: Lee age: 20
(3) for... empty (book_list2 is not defined in views. py)
{% For book in book_list2 % }{{ book }}{% empty %} does not have this list or the list is empty {% endfor %}
Result: The list does not exist or the list is empty.
(4) forloop
Forloop. counter cycle count. The default value is 1.
Forloop. counter0 cyclic count, starting from 0
Forloop. revcounter loop to count. The default value is 1.
Forloop. revcounter0 cyclic count, ends at 0
Forloop. first the bool value of the first loop is True, which is generally used with if
Forloop. last the bool value of the last loop is True, usually used with if
Forloop. parentloop
{% For k in person %} {% if forloop. first %} This is the first loop {% elif forloop. last %} This is the last loop {% else %} {k} {% endif %} {% endfor %}
Result: This is the first loop. name is the last loop.
Previous chapter of the Series: Django Study Notes (4) template Variables
Next chapter of the Series: Django Study Notes (6) MySQL Configuration