django regroup grouper name as choices verbose name - python

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)

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.

Call links list programatically for Pelican site

I have a Pelican blog. I want to call the external links list programmatically, rather than hard code them into the template. For example, the blog post categories are called programmatically, e.g.,
{% for category, articles in categories[0:10] %}
<li>{{ category }}</li>
{% endfor %}
</div>
<div class="l-box pure-u-1-3">
{% for category, articles in categories[11:20] %}
<li>{{ category }}</li>
{% endfor %}
</div>
<div class="l-box pure-u-1-3">
{% for category, articles in categories[21:30] %}
<li>{{ category }}</li>
{% endfor %}
So to be clear, I am looking to change this code to call from a single file which lists some external weblinks.
Assign them to the LINKS variable in your pelicanconf.py e.g.
LINKS = (
('my link 1', 'http://some.link.here'),
('my link 2', 'http://some.other.link.here')
)
and then call them in your template with
{% for name, link in LINKS %}
{{ name }}
{% endfor %}
All variable defined in your pelicanconf.py, as long as they are in all-caps, can be accessed in your templates.
See: http://docs.getpelican.com/en/3.5.0/themes.html#templates-and-variables

accessing a list of tuples in html

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

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