Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Module language in Django

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article shares with you the content of the sample analysis of module languages in Django. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Variables

DTL represents variables in the format {{variable name}}. The variable name consists of letters, numbers, and underscores, but cannot begin with an underscore. When Django renders the template, the variable it encounters is replaced with the corresponding variable value in the context. If the variable is an object, you can use a period () to access its properties or methods.

2. Notes

DTL uses {#... #} to represent comments, and the contents of comments will not appear in the rendering results.

The content commented by {#. #} cannot be wrapped. To comment on multiple lines of content, use the comment tag

Name: {{name}}

{% comment Multiline comment%}

Age: {{age}}

Date: {{now}}

{% endcomment%}

The age and date in the template are included in the comments and are ignored when rendering. For example, sample code that uses the template in a view

Def testTemplate (request): time=datetime.today () c = {'name': "Little Radium",' age':999,'now':time} return render (request,'testtem.html',c)

The browser displays the rendering as shown in the figure:

3. Filter

The filter is used to change the display result of the variable. There are three commonly used filters:

(1) default: sets the variable to false or an alternative value that is displayed when empty. The basic format is {{variable | default: alternative value}}

(2) length: returns the string or list length. The basic format is {{variable | length}}

(3) filesizeformat: converts numeric values to file size format, such as 1.1KB. The basic format is {{variable | filesizeformat}}

(Django provides more than 60 built-in module filters)

4. Tag: include

Tags are used to perform more complex operations. Such as including modules, control flow, create output text or implement template inheritance.

The include tag is used to contain modules, insert additional template code into the current location, and render using the context of the current template.

The basic format of the include tag is:

{% include module name%}

The module name can be a string or a string variable. For example, the template file testtemm.html code

Template A: {{data | default:'nothing'}}

Template file testtem1.html code

{% include 'testtemm.html'%}

Template B: current date: {% now "Y, m / d, H:i:s"%}

The following attempts to use the template testtem.html

Def testTemplate1 (request): return render (request,'testtem1.html', {'data':123})

The {% now%} tag is used in the module testtem.html to get the current date string in the specified format. The browser displays the rendered results:

Parameters can be specified when the module is included

{% include 'testtemm.html' with name=' Little Radium'%}

The parameter is connected after the with, and the parameter name is the same as the variable name in the template. Use spaces as delimiters when passing multiple parameters

{% include 'testtemm.html' with data='abcd' data2=123%}

Example:

Template A: {% include 'testtem.html' with name=' Little Radium'%}

5. Tag: for

Line {{forloop.counter}} of test1 {% for r in data%}: {% for an in r%} {{a}} {% endfor%} {% endfor%}

The following attempts to use the template

Def test1 (request): data= [1 data':data 2, 3 3, 4], [5, 6, 7, 8, 5, 5, 5, 6, 7, 8, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,

The browser displays the rendering as shown in the figure:

You can use reversed to represent a reverse loop

{% for r in data reversed%}

After changing the for tag in the above template file test1.html to reverse loop, render the result (using the reverse loop render result):

For list variables that contain sublists, the for tag maps sublists to independent variables

{% for an endfor d in data%} {{a}}, {{b}}, {{c}}, {{d}} {% endfor%}

For dictionary objects, the for tag maps keys and values, respectively. For example, the dictionary object {'name':' Little Radium, 'age':999} can use the following template:

{% for key,value in data.items%} {{key}} = {{value}} {% endfor%}

The rendering result is as shown in the figure:

You can use {% empty%} inside the for tag block, which represents the output when the object to be traversed does not exist or is empty.

Example:

{% for key,value in data1.items%} {{key}} = {{value}} {% empty%} No dictionary object was found in the context, or data is empty {% endfor%}

Because data1 is an empty dictionary, use {% empty%}

6. Tag: if

The if tag is used to construct conditional branches, and its basic structure is as follows

{% if var1%}. {% elif var2%}. {% else%}. {% endif%}

Elif and else blocks can be omitted, and elif blocks can have multiple. Django calculates the variables of the if and elif tags sequentially, and if the variables are true (and the variables exist, are not empty, and are not False), the corresponding data blocks are output, and the process jumps to the endif tag. If none of the variables are true, output else blocks (if else blocks exist)

For example, the following module outputs grades based on scores:

Score: {{data}}, {% if data > = 90%} Grade: a {% elif data > = 80%} Rank: B {% elif data > = 70%} Rank: C {% elif data > = 60%} Rank: d {% else%} Rank: unqualified {% endif%}

The following view uses this module:

Def test2 (request): data=int (request.GET ['data']) return render (request,'test1.html', {' data':data})

This example gets a score from URL (in formal format), request. The data obtained by GET ['data'] defaults to a string format, so it needs to be converted to an integer. If no conversion is made, Django will use it as a string, and the template will output the data from the else tag section.

Yun visits the http://127.0.0.1:8000/test2?data=88", output in the browser as shown in the figure.

Thank you for reading! This is the end of this article on "sample analysis of module languages in Django". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report