Reputation: 9
I am trying to iterate through a context dictionary in a template in django. So far I've been unsuccessful and I'm not understanding what it is wrong.
This is my view:
def main_view(request):
cat_dict = {'Other': 0,
'Meeting': 0,
'Project 1': 0,
'Project 2': 0,
'Project 3': 0,
'Project 4': 0,
'Collaboration 1': 0,
'Collaboration 2': 0,
'Collaboration 3': 0,
'Process 1': 0
}
my_dict = gCalScriptMain.gCalScript(cat_dict)
return render(request, 'gCalData/gCalData_main.html', context=my_dict)
Instead, this is my template:
{% extends "base.html" %}
{% block content %}
<div class="jumbotron index-jumbotron">
<h1 id="main-title">gCalData</h1>
<ul style="color:white">
{% for k,v in my_dict.items %}
<li>{{ k }}: {{ v }}</li>
{% endfor %}
</ul>
</div>
{% endblock %}
But I get nothing (not even an error). The only thing I can do is retrieve a single value if I put this in the template:
{% extends "base.html" %}
{% block content %}
<div class="jumbotron index-jumbotron">
<h1 id="main-title">gCalData</h1>
<p style="color:white">{{ Other }}</p>
</div>
{% endblock %}
Upvotes: 0
Views: 272
Reputation: 1068
context
that you give to render
function is dictionary of variables you can use in template. This means that you can use Other
, Meeting
, etc.
If you want to use your dictionary, then you need to do
...
return render(request, 'gCalData/gCalData_main.html', context={"my_dict": my_dict})
and then you can iterate over my_dict
in template.
Upvotes: 1