Example of customizing template labels in the Django framework of Python

Source: Internet
Author: User
This article mainly introduces examples of Custom template tags in the Python Django framework. tags are more useful than filters. For more information about how to customize a template tag, see, you need to tell Django how to perform this process when encountering your tag.

When Django compiles a template, it divides the original template into nodes. Each Node is an instance of django. template. Node and has the render () method. Therefore, a compiled template is a list of node objects. For example, look at this template:

Hello, {person. name }}. {% ifequal name. birthday today %} Happy birthday! {% Else %} Be sure to come back on your birthday for a splendid surprise message. {% endifequal %}

The compiled template is in the form of a node list:

  • Text node: "Hello ,"
  • Variable node: person. name
  • Text node: ". \ n"
  • IfEqual node: name. birthday And today

When you call the render () method of a compiled template, the template uses the given context to call the render () method of each node on its node list. These rendering results are combined to form the template output. Therefore, to customize template labels, You need to specify how the original template labels are converted into functions completed by the node (compile function) and node render () methods.

In the following sections, We will detail all the steps for writing a custom tag.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.