In our last tutorial, we have explained how the Django Template System is used.
In this tutorial, we are going to explain a situation when the Django template system encounters a dot in a variable name(in an HTML template file), within an executing template. In such situation, django template System uses the dot lookups strategy.
Dot Lookups Strategy
When the Django Template System encounters a dot in a variable name(at the execution of an HTML template), it uses the dot lookups strategy, which tries to find a specific match for the variable from a list of values shown below(in the following order):
Dictionary lookup
Attribute lookup
Method lookup
List lookup
The first matched lookup type is executed by the Django Template System.
How to implement the dot lookups strategy with dot operator
Dictionary lookup is implemented in the HTML template file using the dot operator, such as - dictionary.key, where dictionary is a Python collection data type dictionary.
Attribute lookup is implemented using the dot operator, such as - object.attribute, where attribute is an attribute of a class, which can be referenced by the object of the class.
Method lookup is implemented using the dot operator, such as - object.method, where object is an object of a built-in class or user-defined class, using which we are accessing a method of the class .
List lookup is implemented using the dot operator, such as - list.index_of_item, where list is a Python collection data type listand we are accessing its item by its index.
Up Next-
In the next few tutorials, we are going to discuss all these lookups strategies used by the Django Template System with examples.
So, please turn to the next tutorial to learn about the first dot lookup strategy used by Django Template System - Dictionary lookup.