Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the method for django assignment_tag to assign tags?

Shulou Source: shulou.com Published: 2022-06-01 07:53:21 10月03日 Update

This article mainly explains the "django assignment_tag assignment tag method is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in-depth, together to study and learn "django assignment_tag assignment tag method is what" it!

Assignment label (save the value of the template variable)

Django.template.Library.assignment_tag ()

To simplify the creation of tags for variables in the setting context, Django provides an auxiliary function, assignment_tag. This function works the same way as simple_tag, except that it stores the result of the tag in the specified context variable instead of outputting it directly.

Our previous current_time function can thus be written as follows:

Register.assignment_tagdef get_current_time (format_string): return datetime.datetime.now () .strftime (format_string)

You can then use the name of the variable followed by the as parameter to store the result in the template variable and output it to where you see fit:

{% get_current_time "% Y-%m-%d% iRu% M% p" as the_time%}

The time is {{the_time}}.

If your template tag needs to access the current context, you can use the takes_context parameter when registering the tag:

@ register.assignment_tag (takes_context=True) def get_current_time (context, format_string): timezone = context ['timezone'] return your_get_current_time_method (timezone, format_string)

Note that the first argument to the function must be called context.

For more information about how the takes_context option works, see include tags.

The assignment_tag function can accept any number of positional and keyword arguments. For example:

Register.assignment_tagdef my_tag (a, b, * args, * * kwargs): warning = kwargs ['warning'] profile = kwargs [' profile']. Return...

Then in the template, you can pass any number of parameters separated by spaces to the template tag. As in Python, the value of the keyword parameter is set with the equal sign ("=") and must be provided after the positional parameter. Example:

{% my_tag 123 "abcd" book.title warning=message | lower profile=user.profile as the_result%} Thank you for reading, this is the content of "what is the method of django assignment_tag assignment tag". After the study of this article, I believe you have a deeper understanding of what the method of django assignment_tag assignment tag is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Tags parameters functions variables templates methods up and down context mode learning location keywords content quantity results storage work output different Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Microsoft MySQL Docker NVidia