Rename grouper in regroup - django

I have read this, but I have one more question: how I can rename country.grouper in the template? I 'group' week days - week_days.grouper, its like 1, 2, 3 etc. But I want change it in the template - Sunday, Monday etc. How to do it?

A custom template tag should help you out here. It has to be put into a file e.g. my_custom_filters.py in your app in a folder called templatetags:
from django import template
register = template.Library()
def get_weekday(value):
"""returns the weekday for the given number - 0 indexed"""
wd_list = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
return wd_list[int(value)]
This custom filter will be used like this:
{% load my_custom_filters %}
{% regroup day by weekdays as weekday_list %}
<ul>
{% for day in weekday_list %}
<li>{{ day.grouper|get_weekday }}
<ul>
{% for item in day.list %}
<li>{{ item.name }}: {{ item.population }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
If you are using a date object could use the build-in date template tag to convert a date to the corresponding weekday:
{% regroup day by weekdays as weekday_list %}
<ul>
{% for day in weekday_list %}
<li>{{ day.grouper|date:"w" }}
<ul>
{% for item in day.list %}
<li>{{ item.name }}: {{ item.population }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>

Related

Grouping dates in Django templates

im trying to group my objects by year only year in datetime field.
{% regroup notificacoes by criados|date:"Y" as criados_list %}
<ul>
{% for x in criados_list %}
<li>
{{ x.grouper }}
<ul>
{% for y in x.list %}
<li>{{ y.slug }} : {{ y.arquivo }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
this was supposed to work, but is not working

How to pass a date (year) parameter to my url?

This is a huge rookie mistake but I can't figure it out.
This is what I wanna do:
I have a page displaying a list a years where an objects is available.
I want that, when I click on a year, it takes me to the corresponding YearArchiveView. I just don't succeed in passing the right parameter to the URL. Passing a template tag obviously doesnt work so what is the right way to do it ?
I get this error:
TemplateSyntaxError at /receipts/
Could not parse some characters: |{{y||date:"Y"}}
My template:
<ul>
{% for y in years_available %}
<li>{{y|date:"Y"}}</li>
{% empty %}
<p>No Receipt Yet</p>
{% endfor %}
</ul>
My view:
class ReceiptListView(LoginRequiredMixin, ListView):
model = Receipt
template_name = 'receipts.html'
def get_queryset(self):
queryset = Receipt.objects.dates('date_created','year',order="DESC")
return queryset
def get_context_data(self, *args, **kwargs):
context = super(ReceiptListView, self).get_context_data(**kwargs)
context['years_available'] = Receipt.objects.dates('date_created',
'year', order="DESC")
return context
My urls.py:
url(r'receipts/(?P<year>[0-9]{4}/$)',views.ReceiptYearArchiveView.as_view(),
name='receipt_year_archive'),
you dont need year=
just use this
<ul>
{% for y in years_available %}
{% with y|date:"Y" as current_year %}
<li>{{y|date:"Y"}}</li>
{% endwith %}
{% empty %}
<p>No Receipt Yet</p>
{% endfor %}
</ul>
You can't add another {{ and }} inside {%. It should call with direct variable.
<ul>
{% for y in years_available %}
<li>{{ y|date:"Y" }}</li>
{% empty %}
<p>No Receipt Yet</p>
{% endfor %}
</ul>
But, I think your case similiar with this docs examples:
<ul>
{% for yearvar in year_list %}
<li>{{ yearvar }} Archive</li>
{% endfor %}
</ul>
If the output of years_available is a list of integer years.
eg: [1992, 2001, 2005, 2011, 2014]
It should be:
<ul>
{% for y in years_available %}
<li>{{ y }}</li>
{% empty %}
<p>No Receipt Yet</p>
{% endfor %}
</ul>

django template print only first unique occurance

I have a pretty simple task to do but I do not know how to do it most effectively in django templates.
Basically, this is what I am trying to accomplish:
Jan. 27, 2015
first post
second post
third post
Jan. 28, 2015
another post
and other
etc
Feb. 18, 2015
again
and again
This could be its model:
class Post(models.Model):
date=models.DateField()
name=models.CharField()
In views.py passing just queryset as posts:
Post.objects.filter(date__gte = date.today()).order_by("date")
And then in template priting it out with for cycle:
{% for post in posts%}
{{post.name}}
{% endfor %}
How do I get printed unique date only once? Is it possible to do this in templates or do I have to take care of it in views? I found similar two year old post.
There is an inbuilt template tag for that - regroup!
This isn't perfect, but with the documentation it should get you close.
{% regroup posts by date as date_list %}
<ul>
{% for date in date_list %}
<li>{{ date.grouper }}
<ul>
{% for item in date.list %}
<li>{{ date.name }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
You can use {% ifchanged %} template tag:
{% for post in posts %}
{% ifchanged %}<h3>{{ post.date }}</h3>{% endifchanged %}
<div>{{ post.name }}</div>
{% endfor %}

Django: How to write an if statement

I want to get the list of songs under the list of artists.
My artist class just contains the artists first and last name.
My song class contains a foreign key of an artist along with the song title.
I am able to list the artists but when I try to list the songs of the artist I get and error in my {% endif %} that ends my if statement {% if song %}.
{% extends "base.html" %}
{% block heading %}Music Catalog{% endblock %}
{% block content %}
{% if user.username %}
<p>Welcome {{ user.username }}!</p>
{% if artist %}
<u1>
{% for singer in artist %}
<li>{{ singer.firstname }} {{ singer.lastname }}</li>
{% if song %}
<u1>
{% for songs in song %}
{% if (songs.artist.firstname == singer.firstname
and songs.artist.lastname == singer.lastname) %}
<li>{{ songs.title }}</li>
{% endif %}
{% endfor %}
</u1>
{% endif %}
{% endfor %}
</u1>
{% else %}
<p>No artists were found in the music catalog.</p>
{% endif %}
{% else %}
<p>You need to login to see your music catalog.</p>
{% endif %}
{% endblock %}
enter code here
I don't think so, in the template language if statement you can use round bracket it won't parse. Try by removing it...
{% if songs.artist.firstname == singer.firstname and songs.artist.lastname==singer.lastname%}
It seems that your view should be handling more of this logic. Like Raunak Agarwal mentioned if you are passing your song or songs in to the template then each one is going to be the same.
It's very strange as well to be doing a
{% for songs in song %}
That just doesn't read right.
I would visit the view a little closer. I had written some more below. After looking over your code though taking a look at the view as well as the model would shed some light on things and allow for a much better help/answer to be given.
As you said My song class contains a foreign key of an artist along with the song title. - why don't you just use the regroup feature?
{% regroup song by artist as artist_list %}
<ul>
{% for artist in artist_list %}
<li>{{ artist.grouper }}
<ul>
{% for songs in artist.list %}
<li>{{ songs.title }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
Yes, your if song line is incorrect. It's quite clear from the template that you don't even have a song attribute at that point. Where is it supposed to be coming from? Presumably it's a related set on singer, but you haven't said so in the template.
You probably want something like this:
{% for singer in artist %}
<li>{{ singer.firstname }} {{ singer.lastname }}
{% with songs as singer.song_set.all %}
{% if songs %}
<ul>
{% for song in songs %}
<li>{{ song.title }}</li>
{% endfor %}
</uL>
{% endif %}
{% endwith %}
</li>
{% endfor %}
I've also removed that comparison of artist firstname and lastname, that didn't seem to make sense: you're already iterating through the set of songs from that artist, so no comparison is needed.

Django regroup not working as expected

I have the following view in my django application
def ViewSale( request ):
salecur = Sale.objects.filter(user=2).order_by('sale_date')
return render_to_response('myapp/sale.html',{'salecur':salecur})
my template looks like this
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
When i render the page i get the grouper sale_date.grouper printed, but {{ sale.item }} and {{ sale.qty }} in the inner loop shows nothing! Blank.
What am i missing?
Gath
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_date.list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
See the documentation on regroup:
{% regroup %} produces a list of group objects. Each group object has two attributes:
grouper -- the item that was grouped by (e.g., the string "Male" or "Female").
list -- a list of all items in this group (e.g., a list of all people with gender='Male').