Python系列视频教程: Django【13讲】第四讲 运算符-表达式
step1:模板标签的使用
mytag.html
使用条件标签
<body>
{% if %}
{% else%}
{% endif %}
</body>
<body>
{% if user%}
<h1>name: {{user.name}}</h1>
{% else%}
user not exist用户不存在
{% endif %}
</body>
def mytag(req):
#user={'name':'tom','age':23,'sex':'male'}
user=Person('Alexander',35,'male')
book_list=['python','java','php','web']
return render_to_response('mytag.html',{'title':'my page','book_list':book_list})
user not exist用户不存在
下面去查看api文档
https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#if
Use of both and andor clauses within the same tag is allowed, withand having higher precedence thanor e.g.:
{% if athlete_list and coach_list or cheerleader_list %}
will be interpreted like:
if (athlete_list and coach_list) or cheerleader_list
Use of actual parentheses in the if tag is invalid syntax. If you need them to indicate precedence, you should use nestedif tags.
> < == != >= <=
in
not in
step2:使用for循环
{% for book in book_list %}
<li>book:{{book}}</li>
{% endfor %}
user not exist用户不存在
- book:python
- book:java
- book:php
- book:web
step3:对字典的遍历
恢复user变量的传递
原先的字典改名为user_dict,一起传递
def mytag(req):
user_dict={'name':'tom','age':23,'sex':'male'}
user=Person('Alexander',35,'male')
book_list=['python','java','php','web']
return render_to_response('mytag.html',{'title':'my page','user':user,'book_list':book_list,'user_dict':user_dict})mytag.html
<body>
{% if user%}
<h1>name: {{user.name}}</h1>
{% else%}
user not exist用户不存在
{% endif %}
<div>
the {{user.name}} say:{{user.say}}
</div>
{% for book in book_list %}
<li>book:{{book}}</li>
{% endfor %}
{% for k in user_dict %}
<li>user_dict:{{k}}</li>
{% endfor %}
</body>
http://127.0.0.1:8000/blog/mytag/
name: Alexander
the Alexander say:I'm Alexander - book:python
- book:java
- book:php
- book:web
- user_dict:age
- user_dict:name
- user_dict:sex
使用KV对
{% for k,v in user_dict.items %}
<li>{{k}}:{{v}}</li>
{% endfor %}name: Alexander
the Alexander say:I'm Alexander - book:python
- book:java
- book:php
- book:web
- age:23
- name:tom
- sex:male
step4:for的进一步使用
for
The for loop sets a number of variables available within the loop:
Variable Description forloop.counter The current iteration of the loop (1-indexed) forloop.counter0 The current iteration of the loop (0-indexed) forloop.revcounter The number of iterations from the end of the loop (1-indexed) forloop.revcounter0 The number of iterations from the end of the loop (0-indexed) forloop.first True if this is the first time through the loop forloop.last True if this is the last time through the loop forloop.parentloop For nested loops, this is the loop surrounding the current one {% for k,v in user_dict.items %}
<li>{{forloop.counter}}.{{k}}:{{v}}</li>
{% endfor %}name: Alexander
the Alexander say:I'm Alexander - book:python
- book:java
- book:php
- book:web
- 1.age:23
- 2.name:tom
- 3.sex:male
使用empty
optional {% empty %} clause whose text is displayed if the given array is empty or could not be found:
{% for k,v in user_dict.items %}
<li>{{forloop.counter}}.{{k}}:{{v}}</li>
{% empty %}
<li>Sorry, no user in this list.</li>{% endfor %}