Greeting, I want to populate a HTML table using multiple jinja dictionary reference but doing it like my code below will create duplication instead because of the for loop inside a for loop, how can I fix this, is there any front-end way, or I have to change my function in view.py?
Any help is much appreciated, thanks
view.py
for employee_id in get_employee_id_list:
get_employee_data = WorkOrder.objects.filter(assign_to=employee_id)
total_task = get_employee_data.__len__()
total_employee_task_list.append(total_task)
context = {
"employee_list": get_employee_id_list,
"total_employee_task_list": total_employee_task_list,
}
return render(request, 'report.html', context)
HTML
<tbody>
{% for employee in employee_list %}
{% for total in total_employee_task_list %}
<tr>
<td>{{ employee.official_name }}</td>
<td>{{ total }}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
Related
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.
am Getting this error i cant figure a way to solve it here are the views.py
class SellerTransactionListView(ListView):
model = Transaction
template_name = "sellers/transaction_list_view.html"
def get_queryset(self):
account = SellerAccount.objects.filter(user=self.request.user)
if account.exists():
products = Product.objects.filter(seller=account)
return Transaction.objects.filter(product__in=products)
return []
template transaction_list_view.html
{% extends "base.html" %}
{% block content %}
<h1>Transactions</h1>
<ul>
{% include "sellers/transaction_list.html" with transaction_list=object_list %}
</ul>
{% endblock %}
and the transaction_list.html
<table>
<thead>
<th>Product</th>
<th>User</th>
<th>order_id</th>
<th>Sale Total</th>
<th></th>
</thead>
<tbody>
{% for trans in transaction_list %}
<tr>
<td>{{ trans.product }}</td>
<td>{{ trans.profile }}</td>
<td>{{ trans.order_id }}</td>
<td>{{ trans.amount }}</td>
<td>{{ trans.timestamp|timesince }} ago</td>
</tr>
{% endfor %}
</tbody>
</table>
if i change the transaction_list_view.html the include part to
{% include "sellers/transaction_list.html" with
transaction_list=transactions %}
the error disappears but the transactions are not showing.
The accounts is a QuerySet, that means that it is a collection that contains zero, one or more SellerAccounts, so you should use:
products = Product.objects.filter(seller__in=account)
so with the __in lookup [Django-doc].
That being said, you can make the above query more effective by writing it as:
def get_queryset(self):
return Transaction.objects.filter(product__seller__user=self.request.user)
Here you thus will return the Transactions that have a product that have a seller that has as user the self.request.user.
I'm trying to parse a Python dictionary into HTML and display it as a table.
I have an SQL database that I draw data from using Python, then parse it into my page like so:
tusername = query_db("SELECT t_username FROM teacher")
return render_template('teachers.html.',username=tusername)
I found this piece of code here, which prints the data:
<table>
{% for t in table %}
<tr>
{% for i in t %}
<td>{{ i }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
This is what it prints:
t_username
t_username
t_username
t_username
t_username
This is my data:
[{'t_username': '1'}, {'t_username': 'admin'}, {'t_username': 'dfsdsfdf'}, {'t_username': 'NewGuy'}, {'t_username': 'test'}]
I want it to print the proper names rather than "t_username". I've not been able to find a solution and I've only just begun working with Python, so I'm not very familiar with it. Anyone able to help?
Provided that t is a dictionary, {% for i in t %} iterates over it's keys (i.e. a single t_username key). Instead just output the value by the key:
{% for t in table %}
<tr>
<td>{{ t.t_username }}</td>
</tr>
{% endfor %}
I've got an HTML template with a Flask Jinja for loop in it which generates a table and looks like:
<tbody>
{% for segment in segment_details %}
<tr>
<td>{{segment}}</td>
<td>{{segment_details['{{segment}}']}}</td>
</tr>
{% endfor %}
</tbody>
I'm trying to iterate through a document of varying length/keys and present each row in the table as the key and value. In my Python code I've got this which has the desired response in the shell:
for item in segment_details:
print(item, segment_details[item])
But in Flask I get the item correctly listing all the rows but the
{{segment_details['{{segment}}']}}
Isn't producing any values, I've tried with and without the single quotes. Is this possible?
This is where your error is:
<td>{{segment_details['{{segment}}']}}</td>
There is no need for the {{ }} inside.
It should be just:
<td>{{segment_details[segment]}}</td>
For more see the documentation for Jinja.
When you are writing a statement(if, for) in Jinja2 you use {% statement %} but when you are accessing a variable then just use {{ variable }}.
it is a solution
<tbody>
{% for key, segment in segment_details.items() %}
<tr>
<td>{{ key }}</td>
<td>{{ segment }}</td>
</tr>
{% endfor %}
</tbody>
Now,I have a dict in view and want to present in HTML in the form of table:
and I pass it into context:
context['cities']=cities
then,render it:
render(request,'index.html',context=context)
In 'index.html':
<table>
<tr>
<th>name</th>
<th>population</th>
<th>country</th>
</tr>
{% for city in cities%}
<tr>
<td>{{city.name}}</td>
<td>{{city.population}}</td>
<td>{{city.country}}</td>
</tr>
{% endfor %}
</table>
Unfortunately,it seems that Django template can't support this kind of traverse,so the result can't show successfully as I expected.
"td" tags are empty.
did you try something like that:
render(request,'index.html',context={
"cities": cities
})
Also, you can try into your template:
{% for city in cities %}
<tr>
{% for key, value in city %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Where key will be name, population, etc