Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Populating an HTML table from Django views.py

New member
Joined
Feb 14, 2023
Messages
6
I have an HTML table that I want to be populated from views.py. Here is my code:

index.html

Code:
{% for pizza in pizza %}
   <tr id="{{pizza.name}}">
       {% for item in pizza.pizza.all %}
            <td>{{item.status}}</td>
            <td>{{item.name}}</td>
       {% endfor %}               
   </tr>
{% endfor %}

views.py

Code:
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?
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.
 
New member
Joined
Feb 14, 2023
Messages
6
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:

Code:
def pizza(request):
   pizza_data = [{'name': 'Pepperoni Pizza', 'status': 'Ready'}]
   return render(request, "index.html", {'pizzaz': pizza_data})

Code:
{% for pizza in pizzas %}
   <tr id="{{pizza.name}}">
       <td>{{item.status}}</td>
       <td>{{item.name}}</td>           
   </tr>
{% endfor %}
 
Top