Template language for Django intermediate articles

Source: Internet
Author: User
Tags comment tag

Template

One, reference variable

Template creation process, for the template, in fact, is to read the template (which nested template tags), and then insert the data obtained in the model into the template, and finally return the information to the user.

{{XXX}}

Second, the label

denoted by {%} to handle some logic

Common several tags {% if condition%}    contents {% endif%}{% for  xxx in  condition%}    {{xxx}}{% endfor%}    

1. For label

1> in each cycle, the template system renders all content between {% for%} and {% endfor%}

2> adds a reversed to the label so that the list is reversed

{% for athlete in athlete_list reversed%}

3> can be nested using {% for%} tags

4> Django does not support exiting a looping operation. If we want to exit the loop, we can change the variable being iterated so that it contains only the items that need to be iterated. Similarly, Django does not support the continue statement, and we cannot let the current iterative operation Jump back to the loop header.

5> has a template variable called ' Forloop ' in each ' {% for%} ' loop. This variable has some properties that indicate looping progress information.

The Magic Forloop variable is only available in loops, and forloop disappears when the template parser reaches {% endfor%}.

Forloop.counter is always an integer counter that represents the number of executions of the current loop Forloop.counter0 similar to Forloop.counter, But it is a Boolean value that is counted from 0 Forloop.first, which is set to Trueforloop.last when the loop is first executed, and is set to True when the loop is last executed

2. If label

The 1>{% if%} tag calculates a variable value, if it is "true", that is, it exists, is not NULL, and is not false, the system displays all contents between {% if%} and {% endif%}

2> No {% elif%} tag, using nested {% if%} tags can do the same thing

3>{% if%} tags accept and,or or not to test multiple variable values or negate a given variable

4>{% if%} tag does not allow both and and or in the same tag, otherwise the logic is prone to ambiguity

5> If you want to combine and and or to make advanced logic, just use the nested {% if%} tag to

6> multiple use of the same logical symbol is legal

Third, template inheritance

Templates can be reused in a way that inherits. Reduce duplicate and redundant code for shared page areas (such as site navigation) throughout the site

In essence, template inheritance is to construct an underlying framework template and then overload it with the common parts and definition blocks of the site in its child template, the first step is to define the underlying template, which will inherit

Master version:

We call it base.html, and define some simple HTML skeleton documents that you can use in some simple two-column Web pages. The task of the "Child" template is to fill in these blank content blocks with content

Child version:

{% extends "text.html"%} {% block title%} modified title {% Endblock%}{% block side%}    <ul>        <li><a href= "/index.html" > Modified home </ a></li>        <li><a href= "/blog/index.html" > post-Edit blog </a></li>    </ul>{% Endblock%}{% block content%}    

1>{% extends "text.html"%} indicates import Master

2> Each {% block%} tag is to tell the template engine that this piece of content under the template will likely be overwritten by the quilt template, not necessarily overwriting the contents of the master

Iv. Note Tags

1. Django Single-line comment using {# This is a comment #}

Annotations with this syntax cannot span multiple lines, and this restriction is intended to improve the performance of template parsing. In the following template, the output is exactly the same as the template itself (that is, the comment tag is not parsed as a comment):

This is a {# nota comment #}test.

2. Implement multi-line annotations, you can use the ' {% comment%} ' template label

{% Comment%} This is amulti-line comment. {% Endcomment%}

Five, filter

The 1> template filter can modify the variable before it is displayed, and the filter uses the pipe character as follows:

{{Name|lower}}

The 2> filter pipe can be * socket *, which is to say, the output of a filter pipe can be used as input to the next pipe:

{{My_list|first|upper}}
Six, ifequal/ifnotequal label

1. {% ifequal%} label compares two values, when they are equal, displays all values in {% ifequal%} and {% endifequal%} The following example compares two template variables user and CurrentUser:

{% ifequal user CurrentUser%}    

Custom Simple_tag

A. Create the Templatetags module in the app

b, create any. py file, such as: xx.py

View Code

C. Import the xx.py file name created before using the custom Simple_tag HTML file

{% load xx%}

D. Using Simple_tag

{% My_simple_time 1 2 3%} {% my_input ' id_username ' Hide '%}

E, configure the current app in Settings, or Django can't find a custom Simple_tag

View Code

Template language for Django intermediate articles

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.