accessing a list of tuples in html - python

I have a list of tuples called top_5 which are the top 5 users usernames and their corresponding post count.
This is a list that has 5 items and each item has 2 indexes.
{% for user in top_5 %}
{{ user }}
{% endfor %}
returns the user and his post count but is there a way I can get them separately?

Is this a Django template? You can access index values in Django template using the dot notation:
{% for user in top_5 %}
{{ user.0 }} {{ user.1 }}
{% endfor %}
And as mentioned in the comments, unpacking the tuples is also an option:
{% for username, post_count in top_5 %}
{{ username }} {{ post_count }}
{% endfor %}

Related

Django ListView, split list into categories in template, by modifying a "current_category" variable

I don't really know how to phrase my question, but what I'm trying to do is add a title between items to split them into obvious categories on my template. I would like to do this:
{# set variable "current_category" to an empty value first #}
{% for item in items %}
{% if item.category != current_category %}
{{ current_category = item.category }} {# <-- How to do that? #}
<h1>{{ item.category }}</h1>
{% endif %}
<p>{{ item.name }}</p>
{% endfor %}
Then end up with:
<h1>Cat 1</h1>
<p>item</p>
<p>item</p>
...
<h1>Cat 2</h1>
<p>item</p>
..
I saw on similar answers that there are things called custom filters and simple tags but it seems really complicated for something very simple. Is this really the only way to do that?
NOTE: I already ordered the items by category of course
I think you're looking for Django's regroup template tag, which does exactly what you're asking for.
{% regroup items by category as grouped_items %}
{% for category, item_list in grouped_items %}
<h1>{{ category }}</h1>
{% for item in item_list %}
<p>{{ item.name }}</p>
{% endfor %}
{% endfor %}

Django Template Tag Loop dictionary variable

I've reading all the template tags posts regarding the loop variable in the key. Apparently Django does not support loop variable in key and I am not sure how to use the custom template tag.
I wanted to display something like this, but how can I achieve this with {% for i in mData %} loop ?
{{ mData.0.name }}
{{ mData.1.name }}
{{ mData.2.name }}
{{ mData.0.age }}
{{ mData.1.age }}
{{ mData.2.age }}
mData is a list of dictionaries.
mData = { "name":"alex", "age":"12"},{"name":"amy","age":"14"} ...
Considering your data is in a list of dictionaries such as:
my_data = [{"name" : "abc" , "age":20,...}, {} , {}...]
You can access all attributes of each dictionary in your template this way:
{% for dict in my_data %}
<!-- Here dict would be each of the dictionary in my_data -->
<!-- You can access elements of dict using dot syntax, dict.property -->
{{ dict.name }}, {{ dict.age }}, ... {{ dict.property }}
{% endfor %}
Reference links: Django templating language
If you want to structure your elements in the order you specifed, you can do something like this:
Name List:
{% for dict in my_data %}
my_data.name
{% endfor %}
Age List:
{% for dict in my_data %}
my_data.age
{% endfor %}
...
Prpoerty List:
{% for dict in my_data %}
my_data.property
{% endfor %}
Django template tags are intentionally very lightweight so that you don't put too much code into the template itself. If you need to do something complicated like loop over every other entry in the database, you should be setting that up in views.py instead of the template.
For the scenario you described, all you need to do is loop over the list of objects:
{% for data in datas %}
{{ data.name }}
{% endfor %}
{% for data in datas %}
{{ data.age }}
{% endfor %}
This solved the problem for me
{% for d in mData %}
{{ d.name }} {{ d.age }}
{% endfor %}
{% for k, v in mData.items %}
{{ k }} {{ v }}
by the way, PEP8 suggest we name the variable as lower + _, but not hump like javascript or other languages.

django template print out by filter id value

I want to print value by id in database,And don't know which keywords to find in Google.
in my views.py, I send transen = TransEn.objects.all() to template
and this will print all datas from database:
{% for words in transen %}
{{words.words|safe }}
{% endfor %}
But I want to print by the value of the id Like:
(Because they are words in English for translating website)
I don't know how to write this in template, please guide me, Thank you very much.
<div><span> TransEn.objects.filter(id='2') </span></div>
<div> TransEn.objects.filter(id='3') </div>
UPDATE:
I have found a method:
I can use if tag, but are there another ideas??
<div>
{% for words in transen %}
{% if words.id == 2 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
<div>
{% for words in transen %}
{% if words.id == 3 %}
{{ words.words|safe }}
{% endif %}
{% endfor %}
</div>
If you want to access each item in the QuerySet individually, by index, you should cast it to a list first. You should change your views.py to:
transen = list(TransEn.objects.all())
And then in your template you can access them by index like so:
<div><span> {{ transen.1.words }} </span></div>
<div> {{ transen.2.words }} </div>
A warning from the Django docuemtnation about casting a QuerySet to a list:
Be warned, though, that this could have a large memory overhead, because Django will load each element of the list into memory. In contrast, iterating over a QuerySet will take advantage of your database to load data and instantiate objects only as you need them.

django regroup grouper name as choices verbose name

Taking simple regroup example from Django documentation:
{% regroup cities by country as country_list %}
<ul>
{% for country in country_list %}
<li>{{ country.grouper }}
<ul>
{% for item in country.list %}
<li>{{ item.name }}: {{ item.population }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
If country.grouper is a Char field declared in model with choices=CHOICES_FIELD, how can I display it's verbose name in template ? Normally i would take Model.get_FOO_display but country.grouper.get_country_list_display of course will not work.
Is custom template tag only choice ?
You simply have to look at it the other way around!
Use get_FOO_display as the grouping field.
{% regroup cities by get_country_display as country_list %}
{{ country.grouper }} will now display the value fields from the
choices set rather than the keys.
(taken verbatim from djangodocs)

Django regroup grouper

I have successfully implemented the regroup call in my template to order missed donations by a pickup_id. The goal was to display all the routes where a a missed donation occured, and a list of all the names underneath the route name. Pickup routes can have the same name so I was grouping by pickup_id. When I do that and call {{ route.grouper }} it returns the ID of the pickup of course. How can I call the field 'route' which displays the route name from grouper?
I was trying things like this...
{{ route.grouper.route }}
{{ route.route.grouper }}
view
missed_routes = Donor.objects.filter(missed='YES').order_by('pickup_id')
template
{% regroup missed_routes by pickup_id as missed_pickups %}
{% for route in missed_pickups %}
<p>{{ route.grouper }}</p>
<ul>
{% for donor in route.list %}
<li>{{ donor.last_name }}</li>
{% endfor %}
</ul>
{% endfor %}
Grouper is just a string, so you have to get name from route instance. Not sure it's gonna work, but try {{ route.list.0.pickup.name }} (I assume that pickup is foreign key to Pickup model with name field) instead of {{ route.grouper }}

Categories

Resources