Django Basic Template Tags

Source: Internet
Author: User

Label

The following sections provide an overview of common Django tags.

If/else

The {%if%} tag tests for a variable value, and if the result is true, the system will display everything between {%if%} and {%endif%}, see example:

If Today_is_weekend%}    <p>welcome to the weekend!</p>{% endif%} is    if today_is _weekend%} <p>welcome to the weekend!</p>else%} <p>get back to work.</p>{% endif%}        

{%if%} tags accept and,or,not to test for multivariable, refer to the following example:

{%If Athlete_listand coach_list%} Both AthletesandCoaches is available. {% ENDIF%}{%IfNot athlete_list%} There is no athletes. {% ENDIF%}{%if athlete_list or coach_list%  There is some athletes or  some coaches. {% endif%}{% if not Athlete_list or coach_list%} There is no athletes or there is some coaches.{ % endif%}{% if athlete_list and not coach_list%and absolutely no coaches.{ % endif%}                

{%if%} tags do not accept and and or are present in a label statement, because this can cause ambiguity. For example:

or Cheerleader_list%}

Parentheses are not supported here, and if you need to, consider putting logic in the outer layer of the template and passing the specified template variable as a result. or nested {%if%} tags:

If athlete_list%}    or cheerleader_list%}    or cheerleaders! {% ENDIF%}{% endif%}     

It is possible to use the same operator multiple times, but it is not possible to use multiple different operators at the same time. The following statement is valid:

or Teacher_list%}

Without the {%elif%} tag, the same thing is done with a nested {%if%} tag:

If athlete_list%}    <p>here is the athletes: {{athlete_list}}.</p>else%}    <p> No athletes is available.</p>if coach_list%} <p>here is the coaches: {{coach_list}}.</p>< c15> {% endif%}{% endif%}     

Make sure that each {%if%} corresponds to a {%endif%}, or Django throws a Templatesyntaxerror exception.

For

{%for%} allows you to loop through each item in a sequence, in the form of a for x in Y,y is the set to traverse, and X is the variable name. For Each loop, the template system will display the contents of {%for%} and {%endfor%}.

For example:

<ul> in    athlete_list%}    <li>{{athlete.name}}</li>    {% ENDFOR% }</ul>   

You can also reverse-traverse a collection:

In athlete_list reversed%} ... {% ENDFOR%}

You can also nest:

In athlete_list%}        athlete.sports_ Played%} <li>{{sport}}</li> {% endfor%} </ul>{% endfor%}   

A common pattern is to check the size of the list before iterating, and to output some special text if the list is empty

If athlete_list% inathlete_list%}<p>{{athlete.name}}</p>{% endfor%Else%}& Lt;p>there is no athletes. Only computer programmers.</p>{% endif%}     

Because this method is too common, the for tag supports an optional {%empty%} option that lets you define the output custom text. For example:

In athlete_list%}<p>{{athlete.name}}</p>{% empty%}<p>there is no athletes. Only computer programmers.</p>{% endfor%}   

Note that the for label does not support Break,countinue.

In the {%for%} tab, you can access the Forloop variable, which has several common properties:

1.forloop.counter: How many times are used to record loops

2.forloop.counter0: Similar to Forloop.counter0, just starting from 0.

3.forloop.revcounter: Number of items used to record an item that has not been traversed

4.forloop.revcounter0: Similar to Revcounter, but counting is starting from 0

5.forloop.first: A Boolean value that identifies whether the first item

6.forloop.last: Boolean value to indicate whether the last item

Ifequal/ifnotequal

The {%ifequal%} tag compares two values and displays all the code between {%ifequal%} and {%endifequal%} if they are equal.

{% ifequal user CurrentUser%}{% endifequal%} 

The arguments can be hard-coded, so the following examples are valid:

'sitenews'%}{% endifequal%}   

{% ifequal section "community"%}
{% endifequal%}

Similar to {%if%}, the {%ifequal%} tag also supports option {%else%}

'sitenews'%}Else%}{% Endifequal%}     

Only template variables, characters, integers, and floating-point types can be used as comparison parameters, here are some valid examples:

{% ifequal variable 1%}{% ifequal variable 1.23%'foo'%"foo"%}
          

Other types, such as list, dictionary, and Boolean types, are not allowed as parameters of {%ifequal%}.

{% ifequal variable True%}{% ifequal variable [1, 2, 3]%}{% ifequal variable {'key'value'}% }

These are invalid parameters. If you want to test if something is true or false, use {%ifequal%}

Comments

Use {# #}, multi-line comments using {%comment%} and {%endcoomment%}

Filter filters

Template filtering is the simplest way to change the value of a variable before displaying them. For example: {{name|lower}}, this will first change the value of name to lowercase and then display it again.

Filters can be used in multiple successive uses:

{{My_list|first|upper}}

Django Basic Template Tags

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.