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

How to understand variables and filters in Django4.1_template

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you how to understand the variables and filters in Django4.1_template, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

I. brief introduction

In the actual development of web, in order to standardize the coding and facilitate the later project planning, we require the separation of front and rear ends, and the front end further requires the separation of layout (HTML), CSS (style) and JS (behavior). In django, we separate template as a separate module to deal with HTML. This chapter mainly explains the use of template module in django.

II. Template syntax

Template composition: HTML+ logic code

III. Logical code of the template

1. The principle of template rendering

Two concepts:

Template: templat

Context: context

Rendering principles (passes):

A.Django first generates an instantiated template object through Template

B.Diango then generates an instantiated context object through Context

C.Djangi calls the rendering method (for example: render ()) through the template instance to render the resulting result

As shown in the figure:

The code block is as follows:

From django.template import Template,Context

Template = Template ("welcome to {{name}}.")

Context = Context ({'name':' Zhejiang'})

Template.render (context)

'Zhejiang welcome to.'

Note: the above process is implemented within django and is relatively complex, so we can directly use the method encapsulated by django to achieve this effect, that is, render (request,'template object file', data) or render (request,'template object file', local ())

The same template object can be reused.

two。 Variables of the template

Format: {{varname}}

Wrap the variable name in double quotes in HTML, and pass the corresponding value of the variable through view (there are two ways to pass the value: 1. {key:value}; 2.locals ()).

The example code is as follows:

2.2 types of variables and variable depth query

Variables can be strings, dictionaries, lists, tuples, objects, and so on.

As shown in the figure:

Page effect picture:

Note: deep queries are used in template, that is, only through symbols. (point) make a next-level query and all sequence data types can use normal index query but not square brackets format, only var.index format can be used, var[ index] or dickey] format cannot be used.

2.3

3. Filter of template

Note: this part of the picture is from the network (if there is any infringement, contact to delete)

4. Key: custom filter filter

The above gives us some commonly used filters, and django provides us with good methods, but what if we need functions with specific functions? Django only provides a common method, so we can use the custom method of django's filter.

1. First create a templatetags module in the app you need to use (must have and be a Package file type)

two。 Create a new .py file in the templatetags module, such as mytags.py

3. Customize the corresponding filter function code in the py file, as follows:

The name of from django import templatefrom django.utils.safestring import mark_saferegister = template.Library () # register is fixed and can not be changed # Custom a multi function @ register.filterdef multi (XMagi y): # Custom filter function return x*y# Custom a multi function @ register.simple_tagdef tag_multi (xMagery z): # Custom label function return x*y*z

4. Import the previously created mytags.py file in the HTML file where you want to use the custom filter function, that is: {% load mytags%}

{% load mytags%} myfilter

The age of {{person.name}} is {{person.age}}

The age of {{person.name}} is * 10: {{person.age | multi:10}}

5. Use the custom filter function in the HTML file as follows:

{% load mytags%} myfilter

The age of {{person.name}} is {{person.age}}

The age of {{person.name}} is * 10: {{person.age | multi:10}}

{# use custom filter function #}

6. Note: a configure the current app in the INSTALLED_APPS in setting, as shown below, otherwise django will not be able to find the custom mytags.py file. b. Restart the project after creating a new mytags.py file; the c.filter function can only accept 2 parameters (curly braces | the first parameter here).

7. The final display on the browser is as shown in the figure:

8. Summary figure:

The above is how to understand the variables and filters in Django4.1_template. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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