December 2018
Beginner to intermediate
796 pages
19h 54m
English
Dot is a multipurpose operator in Django templates. There are three different kinds of operations: attribute lookup, dictionary lookup, or list-index lookup (in that order).
In Python, first, let's define the context variables and classes:
>>> class DrOct:
arms = 4
def speak(self):
return "You have a train to catch."
>>> mydict = {"key":"value"}
>>> mylist = [10, 20, 30]
Let's take a look at Python's syntax for the three kinds of lookups:
>>> "Dr. Oct has {0} arms and says: {1}".format(DrOct().arms, DrOct().speak())
'Dr. Oct has 4 arms and says: You have a train to catch.'
>>> mydict["key"]
'value'
>>> mylist[1]
20
In Django's template equivalent, it is as follows:
Dr. Oct has {{ s.arms }} arms and says: {{ s.speak }}
{{ mydict.key ...Read now
Unlock full access