To customize a template tag, you need to tell Django how to do this when it encounters 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 a render () method. Thus, 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 behaves as a list of nodes:
- Text node: "Hello,"
- Variable node: person.name
- Text node: ". \ n"
- Ifequal nodes: Name.birthday and today
When you invoke the render () method of a compiled template, the template invokes the render () method of each node on its node list with the given context. The results of these renderings are combined to form the output of the template. Therefore, to customize the template label, you need to indicate how the original template label is converted into a node (compile function) and the node's render () method completes the function.
In the following sections, we will explain in detail all the steps when writing a custom label.