Why is it that when I use an include for an example on my page, there is no result from Django Code showing ?
With screenshot above, you can see between
Loop through items of a list:
and In view.py you can see what cars variable looks like.
that there is no result showing from Django code
As a for loops main page, I have a for_loops_tested.html
file and it uses include for different examples to show on one page, like this:
<h4>For Loop List Dictionaries Example</h4>
{% include 'testing/for_loop_dict.html' %}
testing/for_loop_dict.html
looks like this:
<p>Loop through items of a list:</p>
{% for x in cars %}
<h1>{{ x.brand }}</h1>
<p>{{ x.model }}</p>
<p>{{ x.year }}</p>
{% endfor %}
<p>In views.py you can see what cars variable looks like.</p>
In my views.py
it has this code:
def for_loop_dict(request):
template = loader.get_template('testing/for_loop_dict.html')
context = {
'cars': [
{
'brand': 'Ford',
'model': 'Mustang',
'year': '1964',
},
{
'brand': 'Ford',
'model': 'Bronco',
'year': '1970',
},
{
'brand': 'Volvo',
'model': 'P1800',
'year': '1964',
}]
}
return HttpResponse(template.render(context, request))
and for this example, this is urls.py
path('testing/for_loop_tested', views.for_loop_tested, name='for_loop_tested'),
path('testing/for_loop_tested', views.for_loop_dict, name='for_loop_dict'),
My understanding is that you can use include tag
and with
for variables together, but how is it meant to be done with an example I have shown that uses an array ?