Print user groups - python

I would like to print information about all my users and their groups in a template, like this:
{% for user in users %}
<tr>
<td>(there should be enumerate here - 1,2,3,4 etc...)</td>
<td>{{ user.last_name }}</td>
<td>{{ user.first_name }}</td>
<td>
{% for group in user.groups %}
{{ group }}, </td>
{% endfor %}
</tr>
{% endfor %}
but it doesn't work:
'ManyRelatedManager' object is not iterable
I have two additional questions:
1. How can I easily enumerate users, like this:
<tr>
<td>1</td>
<td>Kowalski</td>
<td>John</td>
...
</tr>
<tr>
<td>2</td>
<td>Smith</td>
<td>John</td>
...
</tr>
...
2. How can I print groups like this:
group1, group2, group3
instead
group1, group2, group3,
(last comma is wrong)
Thank you very much.

you should change
{% for group in user.groups.all %}
{{group}}

The following code should do everything you are asking for:
{% for user in users %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ user.last_name }}</td>
<td>{{ user.first_name }}</td>
<td>
{% for group in user.groups.all %}
{{ group }}
{% if not forloop.last %},{% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
Check out the following link from the official docs for more information about builtin forloop variables: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

Related

How to pass list containing dictionary in html template and access its values

In django i have this list
my_list=[{'id':1,'username':'sometext'},{'id':2,'username':'someothertext'}]
i am passing this list into the template
return render(request,'template.html',{'mylist':my_list})
This is my template.html
{% for i in mylist.items %}
<tr>
<td>{{ mylist.id }}</td>
<td>{{ mylist.username }}</td>
</tr>
{% endfor %}
But by doing this i didn't get the value of id and and username.
mylist is a list, not a dictionary, so you should enumerate with:
{# no .items &downarrow; #}
{% for item in mylist %}
<tr>
{# use item &downarrow; #}
<td>{{ item.id }}</td>
<td>{{ item.username }}</td>
</tr>
{% endfor %}

string formatting in Html

I am creating a template in HTML
{% for product in products %}
<tr>
<td>{{ product.id }}</td>
<td>{{ product.product_name }}</td>
{% for i in quantities %}
{% if forloop.counter == forloop.parentloop.counter %}
<td id="q1">{{ i }}</td>
{% endif %}
{% endfor %}
{% endfor %}
How can I assign a different id to each item in quantities?
Can I use .format just like we do in python?
Like this?
{% for i in quantities %}
<td id="q{{ forloop.counter }}">{{ i }}</td>
{% endfor %}

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 %}

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