Django template language -Tags

Tags

  1. Tags
    # 普通for循环
    
    <ul>
    {% for user in user_list %}
        <li>{{ user.name }}</li>
    {% endfor %}
    </ul>

    The for loop parameters are available:

    Variable Description
    forloop.counter Loop current index value (starting at 1)
    forloop.counter0 Loop current index value (zero)
    forloop.revcounter The current value of the inverted index cycle (starting at 1)
    forloop.revcounter0 Reverse current cycle index (starting from 0)
    forloop.first The current cycle is not the first cycle (Boolean value)
    forloop.last The current cycle is not the last cycle (Boolean value)
    forloop.parentloop This layer of outer loop cycles

    for ... empty

    # If user_list which element is 0 when performing empty
     
    <UL> 
    { % for User in user_list% }
         <Li> the user.name {{}} </ li> 
    { % empty% }
         <Li> empty </ li> 
    { % endfor% }
     </ UL>

     

  2. if the judge

    # IF, elif and the else 
    
    
    { % IF user_list% } 
      the number of users: {{user_list | length}} 
    { % elif black_list% } 
      blacklist number: {{black_list | length}} 
    { % the else % } 
      There are no user 
    { % endif% }
    # Of course, there may be only if and the else 
    
    { % if user_list | length> 5% } 
      seven luxury SUV 
    { % the else % } 
        rickshaw 
    { % endif% } 
    
    # if statement supports and, or, ==,>, <, =! , <=,> =, in , not in, it is, is not determined.

     

  3. with

    # Define an intermediate variable, to give a complex multi-variable aliases. 
    
    # Note that no space around the equal sign. 
    
    { % With business.employees.count Total =% } 
        {} {} Total Total Employee {{ | the pluralize}} 
    { % endwith The% } 
    
    # or 
    
    { % Total% AS with business.employees.count } 
        {} {} Total Employee Total {{ | the pluralize}} 
    { %}% endwith The

     

  4. csrf_token

    This tag is used CSRF protection.

    In the form in the form of a page on which writing {% csrf_token%}

  5. Precautions

    1. Django template language does not support continuous judgment that does not support the following wording:
      {% if a > b > c %}
      ...
      {% endif %}

       

    2. Priority attribute of Django template language is greater than the method
      def xx(request):
          d = {"a": 1, "b": 2, "c": 3, "items": "100"}
          return render(request, "xx.html", {"data": d})

      As above, when we use the render method to render a page, pass a dictionary d key items and there is a default d.items () method, this time in the template language:

      {{ data.items }}

      Default items key will take the d.

Guess you like

Origin www.cnblogs.com/wtil/p/11488523.html