I have an HTML table that I want to be populated from views.py. Here is my code:
index.html
{% for item in pizza %}
<tr id="{{ item.name }}">
<td>{{ item.status }}</td>
<td>{{ item.name }}</td>
</tr>
{% endfor %}
views.py
def pizza(request):
pizza_data = [{'name': 'Pepperoni Pizza', 'status': 'Ready'}]
return render(request, "index.html", {'pizza': pizza_data})
The table doesn’t get populated and I don’t see any error code. Is it the format in pizza_data
? I removed the other {% for %}
loop because Lucas is right that the other loop is useless.
I think I should say that index.html
is inside a folder named templates
. Not sure if this will affect because I declared the STATIC_DIR
into this folder.
The reason why pizza_data
is hardcoded is because that is a JSON file that I need to figure out how to insert but for now I want to see if the {% for %}
loop can populate but it is not.
4
Answers
You have to rename your variable i think. And the second loop is useless, your put a dictionary in context, so you just need to access by key of each element:
Use
.items
to loop through the dictionary:Try this code it’s working
views.py
HTML Table
Output in Browser
Your code at least shows result, However If you get template does not exist error It has another solution.