Passing data into templates
When rendering views, we can also pass in data. Add the following in bold to/movie/views.py
:
… def home(request): return render(request, 'home.html', {'name':'Greg Lim'}) …
We pass in a dictionary with a key-value pair ({'name':'Greg Lim'}
) to home.html
. And in home.html
, we retrieve the dictionary values with the following in bold:
… <body> <h1>Welcome to Home Page, {{ name }}</h1> <h2>This is the full home page</h2> </body> …
{{ name }}
accesses the 'name'
key in the dictionary and thus retrieves the 'Greg Lim'
value. So, if you run the site now and go to the home page, you should see what is shown in Figure 4.3:
In the...