Populating an HTML table from Django views.py - python

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.

Try this code it's working
views.py
def pizza(request):
pizza_data = [{'name': 'Pepperoni Pizza', 'status': 'Ready'}]
return render(request, "index.html", {'pizza_data': pizza_data})
HTML Table
<table class="table ">
<thead>
<th>Item Name</th>
<th>Status</th>
</thead>
<tbody>
{% for item in pizza_data %}
<tr id="{{ item.name }}">
<td>{{ item.name }}</td>
<td>{{ item.status }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Output in Browser

Use .items to loop through the dictionary:
{% for obj in pizza %}
<tr id="{{ obj.name }}">
{% for key, value in obj.items %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}

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:
def pizza(request):
pizza_data = [{'name': 'Pepperoni Pizza', 'status': 'Ready'}]
return render(request, "index.html", {'pizzas': pizza_data})
{% for pizza in pizzas %}
<tr id="{{pizza.name}}">
<td>{{pizza.status}}</td>
<td>{{pizza.name}}</td>
</tr>
{% endfor %}

Your code at least shows result, However If you get template does not exist error It has another solution.

Related

If statement in table in django-templates

say we have table in template
<table class="table">
<thead>
<tr>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
{% if {{student.academic_status}}=="promoted" %}
<td class=text-success>promoted</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
So is it possible using if statement in table in django-templates
Since you are already in a template tag, you should not use curly brackets ({{ … }}) for the variable, so:
{% if student.academic_status == "promoted" %}
…
{% endif %}
The correct way to add if statement in Django Templates
follow me !
if statement
{% if condition %}
{{here you add the key that you using in the views page exactly context}}
{% endif %}

Using while loop with counter in django template

I need to put a counter in while loop in template. So I did:
<tbody>
{% with count=1 %}
{% while count <={{orders_count}}: %}
{% for order in orders %}
<tr>
<td style="width:5%;"></td>
<td>{{count}}</td>
<td>{{order.name}}</td>
</tr>
{% count+=1 %}
{% endfor %}
{% endwhile %}
{% endwith %}
</tbody>
But finaly I have the following error:
Invalid block tag on line 216: 'while', expected 'endwith'. Did you forget to register or load this tag?
You do not need a while loop here, you can simply make use of the |slice template filter [Django-doc]:
<tbody>
{% for order in orders|slice:orders_count %}
<tr>
<td style="width:5%;"></td>
<td>{{ forloop.counter }}</td>
<td>{{ order.name }}</td>
</tr>
{% endfor %}
</tbody>
But slicing, etc. does not really belong in the template. You normally do that in the view.
You can try this :
<tbody>
{% for order in orders %}
<tr>
<td style="width:5%;"></td>
<td>{{forloop.counter}}</td>
<td>{{order.name}}</td>
</tr>
{% endfor %}
</tbody>

Django - Looping through a dictionary using a second variable in template

I'm trying my best not to repeat myself in my code but I'm encountering a problem looping through a dictionary by key in my template.
I have two dicts:
exampledict={'firstkey':firstval, 'secondkey':secondval}
keys=['firstkey', 'secondkey']
keydict={'keys':keys}
In my template I want to loop over the exampledict using the keydict:
<tr>
{% for val in keydict %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
I've noticed this kind of combination of variables doesn't work at all, I tried by using:
{'firstkey':'firstkey'}
And sending that through to the template and later trying
{{ exampledict.firstkey }}
Is there a better way to accomplish what I'm trying to do here?
Edit 1:
Manually going through each key as:
<td> {{ exampledict.firstkey }} </td> <td> {{ exampledict.secondkey }} </td>
Where firstkey and secondkey is the actual dictkey for exampledict works, although it makes for a lot of repetition.
Edit 2:
views.py
def tabletest(request):
exampledict={'firstkey':'firstval', 'secondkey': 'secondval'}
keydict={
'keys':['firstkey', 'secondkey']
}
return render(request, 'MinaFakturor/tabletest.html', {'exampledict':exampledict, 'keydict':keydict})
template
<table>
<thead>
<tr>
{% for val in keydict.keys %}
<th>{{ val }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
<tr>
{% for val in keydict.keys %}
<td>{{ exampledict.val }}</td>
{% endfor %}
</tr>
<tr>
<td>{{ exampledict.firstkey }}</td>
</tr>
</tbody>
</table>
Which produces this result:
If I remove the exampledict.firstkey term, nothing is produced in the table body.

Django favourite/compare feature

Just wondering if Sessions can be used to create a quick compare view of two products on my Django app. I'm listing items for sale and would like a user to be able to 'like' multiple items then have a new view to compare the selected products. Any thoughts?
Thanks
Sure, just assign the list products to a session variable.
Then assign the products list to the template, which could look something like that:
<table>
<tr>
<td></td>
{% for product in products %}
<th>{{ product.title }}</th>
{% endfor %}
</tr>
<tr>
<th>Feature 1</th>
{% for product in products %}
<td>{{ product.feature1 }}</td>
{% endfor %}
</tr>
<tr>
<th>Feature 2</th>
{% for product in products %}
<td>{{ product.feature2 }}</td>
{% endfor %}
</tr>
</table>

Django 'getattr(row,', expected 'empty' or 'endfor''

Code I'm working with:
{% for row in all_rows %}
<tr>
{% for names in all_fields_names %}
<td> {% getattr(row, names) %} </td>
{% endfor %}
</tr>
{% endfor %}
I get error:
'getattr(row,', expected 'empty' or 'endfor'
I've tried:
{% for row, names in (all_rows, all_fields_names) %}
<tr>
<td> {% getattr(row, names) %} </td>
</tr>
{% endfor %}
With no success. Any ideas?
You can't call functions like that inside the template. You'd want to create a custom template tag.
Custom Template Tag:
#register.simple_tag
def getattr(row, field_name, default=None):
return getattr(row, field_name, default)
Call it inside your template like so:
{% for row, names in (all_rows, all_fields_names) %}
<tr>
<td> {% getattr row names %} </td>
</tr>
{% endfor %}

Categories