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 Django customizes tags

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

Share

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

This article mainly introduces Django how to customize the label, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

Custom label:

Source code learning:

Template.Library () .simple_tags ():

Def simple_tag (self, func=None, takes_context=None, name=None): "Register a callable as a compiled template tag. Example: @ register.simple_tag def hello (* args, * * kwargs): return 'world'"

There are three parameters:

Func compiled function (a Python function)

Takes_context will explain in the second part

Name works in the same way as name in the filter () function of the custom filter.

The first part

Get started with custom tags!

Pass parameters when using custom tags in the template!

Implemented through custom tags-show the current time in the specified format (the format of the datetime module)!

(1) Writing the view function:

(ceshi/views.py file)

(2) write template files:

(templates/ceshi/test1.html file)

(3) Custom tag implementation:

(ceshi/templatetags/common_extras.py file)

Format_string receives the parameters passed by the front-end template using the label.

(4) effect display:

Part II

Custom label intermediate operation!

Explain and use the parameter takes_context in the source code (also realize the function realized in the first part)!

The value of takes_context is a Boolean value, and the default is False

If you set it to True, you can use the parameters passed in from the context (that is, the context parameters passed to the template in the view function)!

(1) Writing the view function:

(ceshi/views.py file)

(2) write template files:

At this point, there are no parameters when using custom tags in the template, because the parameters are already obtained in the context!

(templates/ceshi/test1.html file)

(3) Custom tag implementation:

(ceshi/templatetags/common_extras.py file)

Part III

Custom label advanced operation!

Custom include tags!

The ability to include tags-you can display some data by rendering another template!

For example, the following lines of code (which may include dozens of lines of code in this for loop) may be used in many places, and all of them have the same format except for the variable choices, so we can encapsulate this part of the code in an include tag!

{% for i in choices%} {{I}} {% endfor%}

(in fact, it is similar to encapsulating a function, encapsulating a lot of repetitive work and calling it directly when you use it! )

(1) define a test01.html file to store the function code:

(templates/ceshi/test01.html file)

{% for i in choices%} {{I}} {% endfor%}

(2) the definition contains labels:

The parameter passed by the decorator is the template file path where the data is displayed!

Finally, return returns a dictionary object.

(3) use:

(4) effect display:

Thank you for reading this article carefully. I hope the article "how to customize Django tags" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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