Method access and search in Django framework, django framework
The key to traversing complex data structures in the Django template is the period (.).
It is best to use a few examples. For example, assume that you want to pass a Python dictionary to the template. To access the value of the dictionary by using the dictionary key, you can use a period:
>>> from django.template import Template, Context>>> person = {'name': 'Sally', 'age': '43'}>>> t = Template('{{ person.name }} is {{ person.age }} years old.')>>> c = Context({'person': person})>>> t.render(c)u'Sally is 43 years old.'
Similarly, you can use periods to access object attributes. For example, the datetime. date object in Python has the year, month, and day attributes. You can also use the period in the template to access these attributes:
>>> from django.template import Template, Context>>> import datetime>>> d = datetime.date(1993, 5, 2)>>> d.year1993>>> d.month5>>> d.day2>>> t = Template('The month is {{ date.month }} and the year is {{ date.year }}.')>>> c = Context({'date': d})>>> t.render(c)u'The month is 5 and the year is 1993.'
In this example, a custom class is used to demonstrate how to access its attributes by adding a point (dots) to the instance variable. This method applies to any object.
>>> from django.template import Template, Context>>> class Person(object):... def __init__(self, first_name, last_name):... self.first_name, self.last_name = first_name, last_name>>> t = Template('Hello, {{ person.first_name }} {{ person.last_name }}.')>>> c = Context({'person': Person('John', 'Smith')})>>> t.render(c)u'Hello, John Smith.'
Point syntax can also be used to reference * Methods * of objects *. For example, each Python string has the upper () and isdigit () methods. You can use the same period syntax in the template to call them:
>>> from django.template import Template, Context>>> t = Template('{{ var }} -- {{ var.upper }} -- {{ var.isdigit }}')>>> t.render(Context({'var': 'hello'}))u'hello -- HELLO -- False'>>> t.render(Context({'var': '123'}))u'123 -- 123 -- True'
Note that * No * parentheses are used to call a method and parameters cannot be passed to the method. You can only call a method without parameters. (The design concept will be explained later in this chapter .)
Finally, a period can be used to access the List index, for example:
>>> from django.template import Template, Context>>> t = Template('Item 2 is {{ items.2 }}.')>>> c = Context({'items': ['apples', 'bananas', 'carrots']})>>> t.render(c)u'Item 2 is carrots.'
Negative List indexes are not allowed. Template variables such as {items.-1} will cause ''templatesyntaxerror''
Python list type
Note: The Python list is indexed from 0. The index of the first item is 0, the index of the second item is 1, and so on.
The period search rule can be summarized as follows: when the template system encounters a point in the variable name, try to search in the following order:
- Dictionary type search (for example, foo ["bar"])
- Attribute search (such as foo. bar)
- Method call (such as foo. bar ())
- List-type index search (for example, foo [bar])
The first valid type found by the system. This is a short circuit logic.
Multi-level deep Nesting is supported for period search. For example, in the following example, {person. name. upper} is converted into a dictionary type query (person ['name']) and then a method call (upper ()):
>>> from django.template import Template, Context>>> person = {'name': 'Sally', 'age': '43'}>>> t = Template('{{ person.name.upper }} is {{ person.age }} years old.')>>> c = Context({'person': person})>>> t.render(c)u'SALLY is 43 years old.'