In [1]:
from django.template import Template, Context
In [2]:
template = Template('Hi, my name is {{ name }}.')
In [3]:
context = Context({'name': 'Andrew'})
template.render(context)
Out[3]:
In [4]:
# no need to reload/recreate the template
context = Context({'name': 'Ada'})
template.render(context)
Out[4]:
In [5]:
template.render(Context())
Out[5]:
In [6]:
template.render(Context({'name': 'Andrew'}))
Out[6]:
In [7]:
template.render(Context({'Name': 'Andrew'}))
Out[7]:
In [8]:
template = Template(
'{{ ml.exclaim }}!\n'
'she said {{ ml.adverb }}\n'
'as she jumped into her convertible {{ ml.noun1 }}\n'
'and drove off with her {{ ml.noun2 }}.\n'
)
mad_lib = {
'exclaim':'Ouch',
'adverb':'dutifully',
'noun1':'boat',
'noun2':'pineapple',
}
context = Context({'ml': mad_lib})
print(template.render(context))
In [9]:
from django.template import loader
In [10]:
template = loader.get_template('organizer/tag_list.html')
In [11]:
best_list = [
{'name': 'Pirates'},
{'name': 'Ninjas'},
{'name': 'Cowboys'},
]
context = Context({'tag_list': best_list})
print(template.render(context))
In [12]:
from organizer.models import Tag
In [13]:
Tag.objects.all()
Out[13]:
In [14]:
# we don't actually need to reload the template
# code is here merely to emphasize the use of the loader
template = loader.get_template('organizer/tag_list.html')
context = Context({'tag_list': Tag.objects.all()})
print(template.render(context))