Python django Template

Source: Internet
Author: User
Tags processing text

1. The text {person_name} enclosed by two braces is called the variable 2 file surrounded by braces and semicolons ({% if ordered_warranty % }) is Template tag 3. The filter uses the same pipe character (|) as the Unix pipe character. 4. Once you create a Template object, you can use context to pass data to it. A context is a collection of variables and their values. The template uses it to assign values to the template variable tag and execution block tag. 5 variable names must start with English characters (A-Z or a-z) and can contain numbers, underscores, and decimal places. (The decimal point has a special purpose here. We will talk about it later) variables are case sensitive. 6. The key to traversing complex data structures in the Django template is the period (.). You can use periods to access dictionary key values, attributes, indexes, and object methods. 7 {% if %} Label check (evaluate) a variable, if this variable is true (that is, the variable exists, not empty, not Boolean false ), the system displays any content between {% if %} and {% endif %. the {% else %} tag is optional:

8. In the python empty list ([]), tuple (), Dictionary ({}), string (''), zero (0), and None object, false in logical judgment, and true in other cases.

9 The {% if %} tag accepts the and, or not keyword to judge multiple variables, or the variable is reversed (not) and does not have the 10 {% elif %} tag, please use nested {% if %}. Be sure to close every {% if %} tag with {% endif %. 11 {% for %} allows us to iterate over a sequence. Similar to the for statement in Python, the loop syntax is for X in Y, Y is the sequence to be iterated, and X is the variable name used in each specific loop. In each loop, the template system renders all content between {% for %} and {% endfor %}. 12 Django does not support exit loop operations. If we want to exit the loop, we can change the variable being iterated, set a special forloop template variable 14 forloop to include only the project 13 {% for %} tag to be iterated in the loop. counter0 is similar to forloop. counter, but it is counted from 0. This variable is set to 0 when the first loop is executed.

15 forloop. revcounter is the integer variable that represents the remaining items in the loop. When the loop is first executed, forloop. revcounter is set to the total number of items in the sequence. In the last loop, this variable is set to 1.

16 forloop. revcounter0 is similar to forloop. revcounter, but it uses 0 as the end index. During the first execution of the loop, the variable is set to the number of items in the sequence minus 1. In the last iteration, the variable is 0.

17 forloop. first is a Boolean value. This variable is True when the first loop is executed. In the following cases, this variable is useful.

18 forloop. last is a Boolean value. It is set to True when the last loop is executed. A common usage is to place pipeline operators (|) between a series of links)

19. The forloop variable can only be used in a loop. When the template parser encounters the {% endfor %} tag, the forloop variable cannot be accessed.

The 20 {% ifequal %} tag compares two values. When they are the same, all values are displayed in {% ifequal %} and {% endifequal %.

21 is similar to {% if %}. {% ifequal %} supports the Optional {% else %} tag. Only template variables and strings are supported, integers and decimals can be used as the {% ifequal %} tag parameters. Other types, such as the dictionary type, list type, and boolean type of Python, cannot be used.

In {% ifequal %.

22. Like HTML and other languages such as python, the Django template system also allows annotations. Note using {##}

23. The template filter is a simple method for modifying the value of a variable before it is displayed.

23 addslashes: Add a backslash to the front of any backslash, single quotation marks, or double quotation marks. This is useful when processing text containing JavaScript.

24 date: format the date or datetime object according to the specified format string parameter

25 set TEMPLATE_DIRS in settings. py as follows:

Import OS. path

TEMPLATE_DIRS = (

OS. path. join (OS. path. dirname (_ file _), 'templates'). replace ('\\','/'),

)

26 The render_to_response () function in the django. shortcuts module. Most of the time, you will use render_to_response () instead of manually loading templates, creating Context and HttpResponse objects, it loads templates, fills in context, and returns parsed template results as HttpResponse objects.

27 {% include % }. This label allows other templates to be included. The tag parameter is the Template Name To be included. It can be a variable or a string hard-coded with single/double quotation marks. When the same code appears in multiple templates, you should consider whether to use {% include %} to reduce duplication.

28 template inheritance is to first construct a basic framework template, and then reload the common parts of the website and definition blocks it contains in its subtemplates.

The 29 {% block %} tag tells the template engine that subtemplates can reload these parts.

30 {% extends %} tag, which is a sub-template. The template engine immediately loads its parent Template

31 inheritance does not change the working method of context, and you can use multi-layer inheritance as needed. A common method of inheritance is the following three-tier method:

A. Create a base.html template to define the main appearance of the site. These are not often modified or even never modified.

B. Create a base_SECTION.html template (for example, base_photos.html and base_forum.html) for each region of the website ). These templates expand base.html and include regional-specific styles and designs.

C. Create independent templates for each type of page, such as forum pages or image libraries. These templates expand the corresponding regional templates.

32 some tips on using the template inheritance:

A. If {% extends %} is used in the template, it must be the first template tag in the template. Otherwise, template inheritance does not work.

B. In general, the more labels {% block %} in the basic template, the better. Remember, the sub-template does not need to define all the code blocks in the parent template. Therefore, you can fill in some code blocks with reasonable default values, and then define only the code blocks required by the sub-template. As the saying goes, the more hooks, the better.

C. If you find that you have copied code between multiple templates, you should consider placing the code segment in a {% block %} of the parent template.

D. To obtain the content of the code block in the parent template, use the {block. super} variable. This variable is very useful if you only want to add content on the basis of the upper-level code block, rather than reload it all.

E. Multiple {% block %} with the same name cannot be defined in the same template }. This restriction exists because the block label works in two-way. That is to say, the block tag not only mines a pitfall to be filled, but also defines the content filled with this pitfall in the parent template. If two {% block %} tags with the same name appear in the template, the parent template will not know which block to use.

F. {% extends %} uses the same loading method as get_template () for the name of the passed template. That is, the Template Name is added to the TEMPLATE_DIRS setting.

G. In most cases, the {% extends %} parameter should be a string, but this parameter can be a variable if the parent Template Name can be determined until the runtime. This allows you to implement some cool dynamic functions.

 

Related Article

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.