Django framework - Advanced Systems of template

--- --- restore content begins

1. Common Grammar

Remember two special symbols: {} {}   and  {%}% .

Used when applied to variable {{}}, if it is associated with logic, then you use {%%}.

In Django template (Template) in use to {{}}   and  {%}% wording, referred to in the professional Django the "template language" 

2. Variable

In Django templates, when variables used, it is to use this format: {{name}} variable.

In the template template engine encounters a variable, it automatically according to the relevant data in the background of views python module provides to calculate the results of this variable, and replace it with their own results.

Variable name: naming conventions include (characters, numbers, underscores) combination.

Note: The name of the variable must not contain spaces, punctuation.

Dot (.) Has a special meaning in the template language. When the template system encounters a dot ( "."), It will be the priority of such a query:

First: dictionary lookup (Dictionary lookup)

Second: property or method query (Attribute or method lookup)

Third: Digital index query (Numeric index lookup)

Precautions:

  1. If the value of calculation result is called, it will be invoked with no parameters. Result of the call will be the value of the template.
  2. If variables does not exist, the system will insert the value string_if_invalid template option, which is the default setting is '' (the empty string).

Using Variables wording:

{ # Take a first list parameter list} # 
{list.0} {} 

{ # take the dictionary key value d} # 
{d.name} {} 

{ # take the object name attribute} # 
{{person_list .0.name}} {#: (vernacular person a list of objects  of the  first personal objects  of  . "" name), with the point "and" somewhat similar to #}
{ # . Calls not only operating parameters of the method} # {} {} person_list.0.eat 

Note: If the method is invoked, it can not take back brackets ().

3. Filters (filter)

In Django template language by using filters to change the display of variables (result).

The syntax of the filter: {{name | filter_name:}} . Note: use the pipe "|" . name is the result of variables, in fact, variable names.

For example: a variable used in the template as {{name | lower}} then will display its value after the name variable length by filtration (operation). lower in action here is all lowercase text.

Precautions:

  1. Filter supports the "chain" operation. I.e., a filter output as input to another filter.
  2. Filters can accept parameters. For example: {{sss | truncatewords: 30}}, which will display the first 30 words of sss.
  3. Filter parameter contains a space, it must be wrapped in quotes. Such as comma and a space used to connect the elements of a list, such as: {{list | join: ','}}
  4. '|' Around is no space without a space with no spaces

 3.1 default

 例如:{{ name|default:"not found" }}

If the variable name value is false or does not exist or is empty, there is automatically a string instead of the default values ​​not found, or the value of a variable name here automatically "not found".

3.2 length

 

3.3 filesizeformat

 

3.4 slice

 

3.5 date

 

3.6 safe

 

3.7 truncatechars

 

3.8 truncatewords

 

3.9 cut

 

3.10 join

 

3.11 timesince

 

3.12 timeuntil

 

3.13 Custom filter

 

3.14

 

3.15

 

3.16

 

 4. Syntax

4.1 for loop

 

4.2 if judgment

 

4.3 with statement

 

4.4 csrf_token

 

4.5 Notes

 

Master

 

Master inheritance

 

Fast (block)

 

Package

 

Static related files

 

tag

simple_tag

inclusion_tag

 

 

 

 

 

 

 

 

 

 

 

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/mashangsir/p/11432871.html