django template label notes

A template variable note:
1, using the variables in the template, the required variables into '{}' of. '{{Variable}}'
2, if you want to access properties of an object can be produced by "the object. Attribute name 'access to
3, value key corresponding If you want to access a dictionary, then only through' dictionary .key '
4, as also used in access dictionary of 'key' time '' to access and therefore can not as a 'key' in the attribute name itself is defined in the dictionary, or dictionary of attributes that will be key programming dictionary
5 access to, or the way if you want to access a list of tuples, then also by '' access, not by '[]' way.

Second, the front end of the template for the cycle
. 1, 'if' tags: if the label corresponding to the if statement 'Python' is, there 'elif' and 'the else' corresponding, but all Tags are required label symbol ( '{ %%} ') wrapped. You may be used if tag '==',! =, < ,>, <=,> =, in, not in, is, is not ' operator determines the like. Analyzing other operators.
2, 'for ... in ...' label: python and usage of the same, can traverse lists, tuples, strings, dictionaries and all objects can be traversed.
If you want to reverse traversal, so when traversing plus 'reversed'. Example code:

    {% for student in students%}
        <p>{{ student.name }} </p>
        <p>{{ student.age }} </p
    {% endfor %}

3, traversing the dictionary: Code Example:

{% for key,value in students.items %}
<p>{{key}}:{{value}}</p>
{% endfor %}

4, provides for a number of parameters in the loop:
<. 1> forloop.counter: current cycle index. 1 as the start value.
<2> forloop.counter0: current cycle index. 0 as the start value.
<3> forloop.revcounter: current reverse cycle index value. For example, the list has 10 elements, the first time through this property is equal to 10, the second was 9, and so on. 1 and is as a subscript of the last element.
<4> forloop.revcounter0: forloop.revcounter and similar, except that the last element is the index of 0. The
<. 5> forloop.first: whether the first pass for the first line is not used simultaneously for setting.
<6> forloop.last: is the last time through, do not use settings that apply to the same time for the last line.
<7> forloop.parentloop: If there are a plurality of nested loops, then this attribute is represented on a for loop.
5, for ... in ... empty: Use this with for ... in ... the same as that difference is, when traversing the object, if there is no element under the circumstances, will perform 'empty' in content. Sample code:

% { For studet in Students.% }
     <Li> Student {} {} </ Li> 
{ % empty% } 
    Buyer No Student 
{ %} endfor%

Third, the stencil leading end tags used with
variables defined in the template. Sometimes, when a variable access is more complex, then the first complex variables can be cached on a variable, this variable directly after using it.

1     context ={
2         'students': ['小明','小溪']
3     }
4     
5     {% with list1=students.1 %}
6         <p>{{ list1 }}</p>
7     {% endwith %}

When used with tags Note:
1, with the variables defined in the statement, only in {% with%} {% endwith %} can not be used outside of this tag.
2, the definition of variables, you can not leave spaces on both sides about the equal sign. For example {% with list1 = students.1%} is wrong.
3, there is another way to write is also supported by:

1         {% with students.1 as list1 %}
2             <p>{{ list1 }}</p>
3         {% endwith %}

Four, url tags
   in the template, we often write some 'url', when we wrote this dead 'url' hard-coded directly in the page, post up and maintenance costs are high. Then you can use the url tag. Django used like the 'reverse'.

<a href= "{% url'book:list' %}'" > book list page </a>

  How to do if you need to pass parameters to the back end? Then you can use positional parameters or keyword parameters. Remember, not both, and when there are multiple parameters, use the space is divided, do not use ''.

    # Path portion 
    path ( ' Detail / <book_id> / <chapter_id> / ' , views.book_detail, name = ' Detail ' ) 
    
    # front-end url inverted use position parameter 
    <A the href = " {% url 'Book: Detail '11%}' " > book details page </a> # frontend url reverse, use keyword arguments <a href=" {% url'book:detail' book_id=1 chapter_id=1
     %}' "> book details page </a>
    
    

Five, spaceless Tags: remove HTML tags whitespace characters, including spaces, tab key, line and so on. Sample code is as follows:

1     {% spaceless %}
2         <p>
3             <a href='foo/' >Foo</a>
4         </p>
5     {% endspaceless %}

After the rendering:

<p><a href='foo/' >Foo</a></p>

spaceless only remove whitespace characters between html tags. It does not remove whitespace characters between tags and text. E.g:

    <span> 
        does not compress
     </ span>

At this time it is not compressed in the span tag.

Six, autoescape label
on or turn off automatic escaping within the label element. Automatically escaped some special characters can be escaped as character html syntax can be identified. For example <it is escaped as & lt, and> is escaped as & gt. The default template is opened automatically escaped. Example code:

1      # of context information transfer 
2      context = {
 . 3          " info " : " <a href='www.baidu.com'> Baidu </a> " 
. 4          }
 . 5      # stencil off automatically escapes 
. 6      {%% ON autoescape }
 . 7          {{}} info
 . 8      {%}% endautoescape

Then it will appear as a hyperlink Baidu in the text. If the on to off, a character string is displayed as normal.

Seven, Verbatim tags: default render those special characters in the front end of the template, such as {%} and {{% and the like. If you do not want to use the template parsing engine in a code snippet, simply place the code in verbatim label can be.

1     {% verbatim %}
2         {{ if }}
3     {% endverbatim %}

Guess you like

Origin www.cnblogs.com/xshan/p/12114969.html
Recommended