Reputation: 61
{'provide a full refund of any money paid ': ['the ', 'dith ', 'with ', 'ande ', 'cor '], 'copyright laws of the place where you ar': ['e ', 'init ', ' or ', 'ate ', 's '], 'person or entity that provided you with ': ['the ', 'of ', 'ande ', ' or ', 'project '], 'michael s. hart is the originator of the': [' the ', '\n ', 's ', 'r ', ', ']}
How do i parse this django variable passed through my view to the html file. I want to make this data to be displayed in the html file in form of a table where each keys values are shown
return render(request, 'notebook/instant_search.html', output)
i tried this in my html file where out put is the variable i am passing through my view
{% for key, value in output %}
{{ key }} <br>
{% for key2 in value %}
{{ key2 }} <br>
{% endfor %}
{% endfor %}
also this:
{% for k in context %}
{{ k }}
{% endfor %}
But i am not getting any output. Its blank nothing on the screen to display
Upvotes: 3
Views: 6867
Reputation: 705
To start with, your render
function isn't accepting the correct arguments and that is why nothing is appearing on your html template. You typed this:
return render(request, 'notebook/instant_search.html', output)
The correct one:
return render(request, 'notebook/instant_search.html', 'output':output)
The above will solve the problem of template not displaying data from the render function.
Next is the code that will iterate through the dictionary:
The following will display each item in the list
{% for k, v in output.items %}
{% for i in v %}
{{ i }}
{% endfor %}
{% endfor %}
while the code below will display each list
{% for k, v in output.items %}
{{ v }}
{% endfor %}
References: https://docs.djangoproject.com/en/2.0/intro/tutorial03/
https://docs.djangoproject.com/en/2.0/topics/http/shortcuts/#render
Upvotes: 7
Reputation: 61
return render(request, 'notebook/instant_search.html', {"output":output})
Change this statement in the views file and then you will get the output by having
<table>
{% for key, value in output.items %}
<tr>
<td>{{key}}</td>
<td>{{value}}<td> <!-- you can also run for on values list -->
</tr>
{% endfor %}
</table>
Upvotes: 2
Reputation: 743
you can iterate dictionary directly in the template:
<table>
{% for key, value in my_dict.items %}
<tr>
<td>{{key}}</td>
<td>{{value}}<td> <!-- you can also run for on values list -->
</tr>
{% endfor %}
</table>
hope it helps
Upvotes: 1