Python系列视频教程: Django【13讲】第四讲 运算符-表达式

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用户不存在

the say:
  • 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:

    VariableDescription
    forloop.counterThe current iteration of the loop (1-indexed)
    forloop.counter0The current iteration of the loop (0-indexed)
    forloop.revcounterThe number of iterations from the end of the loop (1-indexed)
    forloop.revcounter0The number of iterations from the end of the loop (0-indexed)
    forloop.firstTrue if this is the first time through the loop
    forloop.lastTrue if this is the last time through the loop
    forloop.parentloopFor 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 %}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值