Need help in Django Templates(using forloops) - python

I have 3 models namely Uses, Customer and Services. I have a query whose result is stored in the variable count.
This is the query
counts = Services.objects.filter(uses__customer=customer).annotate(times_used=Count('uses'))
Each count[x].times_used has a variable,x ranges from 0 to infinity.
I have used a for loop in my html to get all the Services which I have passed through context in views.
Now I need to get unique value of count for each service. How can I do this.
Here is my html code
<table class='table table-bordered' id='dataTable' width='100%' cellspacing='0'>
<thead class='bg-primary'>
<tr class='text-white'>
<th>Service-Id</th>
<th>Service-name</th>
<th>Count</th>
<th>Price</th>
</tr>
</thead>
<tbody class="text-dark">
<tr>{% for service in services %}
<td>{{ service.id }}</td>
<td>{{ service.service_name }}</td>
<td>#code for printing count[x],times_used#</td>
<td>{{ service.price }}</td>
</tr>{% endfor %}
</tbody>
</table>

{% for elem_1 in results_1 %}
{{elem_1}}
{% for elem_2 in results_2|index_filter:forloop.counter0 %}
{{elem_2}}
{% endfor %}
{% endfor %}
You can use that structure in your template, anywhere.
You use a builtin filter "index_filter".
Basically, when myvar is piped with | to index_filter:forloop.counter0, then the function will call the element in results_2 with index = forloop.counter0. This later counter being the one of the parent for loop.

Related

Show only "Reorder" item

I am using Django and in the front end, I am trying to show only "Reorder" Parts on the table. To define the status of Available and Reorder, I am normally using the if-else method to track. I was thinking about filtering but I have no such Status field in my DB. Anyone can give me any idea how to do that? Here is what I've done for now
HTML
<tr>
<th class="serial">#</th>
<th>Part No</th>
<th>Part Name</th>
<th>Quantity</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>
{% if parts %}
{% for part in parts %}
<tr>
<td class="serial">{{ forloop.counter }}</td>
<td>{{ part.partno }}</td>
<td>{{ part.partname }}</td>
<td>{{ part.quan }}</td>
<td>
{% if part.quan <= part.limit %}
<p style="color: #FF0000">
Reorder
</p>
{% elif part.quan > part.limit %}
<p style="color:#008000">
Available
</p>
{% endif %}
</td>
<td>{{ part.created_date }}</td>
</tr>
Part Table
You can reference an value from the instance in a filter, this queryset will return your parts that need reorder
from django.db.models import F
parts = Part.objects.filter(quan__lte=F('limit'))

Jinja2 include with batch filter

I'm trying to include a template inside a for loop with a batch filter applied
But I can't figure out how/if I can include for each object in the list that the filter returns
I see people online selecting from the loop like so:
{% for result in results %}
<tr>
<td>{{ result[0] }}</td>
<td>{{ result[1] }}</td>
<td>{{ result[2] }}</td>
</tr>
{% endfor %}
I just can't figure out how to include for each in the list
My code is as follows:
{% for post in posts | batch(2, ' ') %}
<tr>
<td style="Width: 10%; height: auto">
{% include '_post.html' %}
</td>
</tr>
{% endfor %}
Including portions of a template in a loop/filter construction is perfectly fine.
Using your example, to build a table of posts using a partial template for each batch, you'll need:
The template with the loop/filter where you include the partial:
<table>
<thead><tr>
<th>Title</th>
<th>Author</th>
<th>Title</th>
<th>Author</th>
</tr></thead>
<tbody>
{% for row in posts | batch(2) %}
{% include "row.html" %}
{% endfor %}
</tbody>
</table>
The partial template "row.html":
<tr>
<td>{{ row[0].title }}</td>
<td>{{ row[0].author }}</td>
<td>{{ row[1].title }}</td>
<td>{{ row[1].author }}</td>
</tr>
Another option would be to iterate over the batch partition again and use a simpler partial template:
The template:
<table>
<thead><tr>
<th>Title</th>
<th>Author</th>
<th>Title</th>
<th>Author</th>
</tr></thead>
<tbody>
{% for row in posts | batch(2) %}
<tr>
{% for col in row %}
{% include "col.html" %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
And "col.html":
<td>{{ col.title }}</td>
<td>{{ col.author }}</td>
If it's not working, double check the variable names, partial names, etc.

Loop to render Django view context as table in the template

I want to pass the following dictionary as context to Django template:
context = {'prices': prices, 'listings_links': listings_links, 'listings_names': listings_names, 'photo_links': photo_links}
The dictionary's values are lists.
In the template I want to display those lists as columns in HTML table. However I am not sure how to further develop the following skeleton table code:
<table>
<tr>
<th>Price</th>
<th>Link</th>
<th>Listing name</th>
<th>Photo link</th>
</tr>
{% for loop start here? %}
<tr>
<td> {{prices[0] }} </td>
<td> {{ listings_links[0] }} </td>
<td> {{ listings_names[0] }} </td>
<td> {{ photo_links[0] }} </td>
</tr>
#next rows go here...
{% endfor %}
</table>
In the view, zip your lists into a single iterable.
items = zip(prices, listings_links, listings_names, photo_links)
context = {'items': item}
Then you can unpack items in the template:
{% for price, listing_link, listing_name, photo_link in items %}
<tr>
<td>{{ prices }}</td>
<td>{{ listing_link }}</td>
<td>{{ listing_name }}</td>
<td>{{ photo_link }}</td>
</tr>
{% endfor %}

Multicolumn table in django template

How to fill multicolumn table in django template?
I have list of n elements (let's say just numbers there) and I want to create a table (looks like grid) with 5 cells in a row.
Very easy to create table of 1 column, but to create 5? Of course we assume than list may have arbitrary number of items from zero to couple hundreds.
<tbody>
{% for item in data.items.all %}
<tr class="item-{{ item.number }}">{{ item.number }}</tr>
{% endfor %}
</tbody>
Try the following code:
View
data = [1,2,3,4,......,99,100]
Template
<table border="1">
<tbody>
<tr>
{% for item in data %}
{% if forloop.counter|divisibleby:5 %}
<td>{{ item }}</td>
</tr>
<tr>
{% else %}
<td>{{ item }}</td>
{% endif %}
{% endfor %}
</tr>
</tbody>
</table>
This code checks the loop count each time. if it is divisible by 5 then close the current tr tag and add a new one.
This may not be the right way to achieve this.
Hope that helps.

Django - Use forloop.counter0

I would like show element in my list on template html like that :
<table class="table table-striped col-md-12">
<tbody>
{% for row in list %}
<tr>
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ ..... }}</td>
</tr>
{% endfor %}
</tbody>
When the len(list) evolve, I need to call new row manually.
So to have a generic call and dependent of the length on my list I choice the method forloop.counter0, but when I write my code, I have error or no elements on my screen.
<tr>
{% for i in "xxx" %}
<td>{{ row.forloop.counter0 }}</td>
{% endfor %}
</tr>
You shouldn't need to look up the index at all, just have another forloop inside that loops over the row
{% for i in row %}
<td>{{ i }}</td>
{% endfor %}
If you need anything more granular, you might want to look into making an actual model to represent the data, and then iterate over a list of this model.

Categories

Resources