Django template length attribute behaves strange - django

I have encountered a weird behavior. And not sure how I should change it to get what I want.
Example:
{% if chapters|length > 0 %}
Chapters:
<div id="left_menu">
<ul>
{% for chapter in chapters %}
<li>{{ chapter }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
Sometimes however my queryset is empty.
When I get this length in the shell it returns 0. So I assumed this would work.
However, when the queryset chapters is empty it still shows my Chapters: and the div inside the page source.
What is going on here and how can I get the result I want?
chapters:
chapters = Chapter.objects.filter(campaign=campaign).order_by("number")
just a normal queryset that might be empty.

Related

What does this html tag mean in django's example?

This is django's polls demo, and most are well documented. However, in this part:
https://docs.djangoproject.com/en/3.0/intro/tutorial04/
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
Vote again?
The documentation doesn't say anything about this part:
vote{{ choice.votes|pluralize }}
And from the generated html page, I can't see what's the role of this piece?
pluralize is an in-built Django template tag that attempts to convert the word that it is appended to to plural. So you feed it a number, and if the number is 1 then it returns '', but if the number is greater than 1, it returns 's'.
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#pluralize

if-statement doesn't work in for-loop

I have an issue with the if-statement and need some help. This is a short code-snippet in html:
{% for category in categories %}
{% if category == 'christmas' %}
<p>{{category}} 1</p>
{% else %}
<p>{{category}} 2</p>
{% endif %}
{% endfor %}
With the for-loop I walk through categories and check them with an if-statement for the string 'christmas'. The paragraph is always the second one, ending with a 2. Still, it appears that the category with the name 'christmas' comes up. That means, that in the if-statement "category" is different than in the p-tag. In fact, category is empty in the if-statement.
Why? Can someone help here, please. Thanks!
Edit: Added two pictures. On the right you see the output:
length is zero
showing no fit, although it should
I'm guessing you're looping over category objects from a model meaning categories is not a list of strings but a queryset? In that case you should do something like this:
{% for category in categories %}
{% if category.name == 'christmas' %}
<p>{{category}} 1</p>
{% else %}
<p>{{category}} 2</p>
{% endif %}
{% endfor %}
Replace .name by whatever your correct attribute is.
Your {{category}} is probably displayed correctly because of your __unicode__ or __str__ method.

Django template if statement return wrong value

In my home page I have blog, on the right side there is Posts category list like:
Sports
Crime
and etc.. I try to make that when I'm in certain category that active category button will be highlighted.
I'm doing that by setting GET parameter to my home page like: /?category=1
NOW... In index.html template I'm doing this:
{% for category in category_list %}
{{ category.pk }} != {{ request.GET.category }} // This is for debugging. Returns 1==1
{% if category.pk == request.GET.category %}
<li>{{ category }}<span class="pull-right">({{ category.post_set.count }})</span></li>
{% else %}
<li class="active">{{ category }}<span class="pull-right">({{ category.post_set.count }})</span></li>
{% endif %}
{% endfor %}
But this is not working. Any suggestions how to solve this, maybe there is another way?
EDITED:
I figured that category.pk returns int and request.GET.category returns string. Next question. How to convert int<==>string, so I could compare them?
It worked by doing this:
{% if category.pk != request.GET.category|add:"0" %}
apparently |add:"0" converts string to int.

NoReverseMatch when rendering page

I seem to know where the issue is located since I can get around it, but for getting around it I have to sacrifice a function I really want to keep.
Here is the relevant code in the non-working state:
{% if sections %}
{% for item in sections %}
<a class="sections" href="{% url 'sections:generate' item.section.slug %}">{{ item.section.title }}</a>
{% for subsection in item.subsections %}
<p>{{ subsection.title }}</p>
{% endfor %}
{% endfor %}
{% else %}
<p>Error retrieving sections or no sections found</p>
{% endif %}
The problem part above is in the link tag. Let me explain by showing the related view.py:
def index(request):
sections = Section.objects.all()
context = {
'sections': [],
}
for section in sections:
context.get("sections").append(
{
'section': section,
'subsections': get_subsections(section),
}
)
return render(request=request, template_name='index.html', context=context)
So, 'sections' is an iterable list of items, containing for every items a dictionary with two entries. One, 'section' and one 'subsection'. There are multiple subsections for every section, this is what I really want to accomplish.
Ordinarily, when not bothering with subsections and simply iterating over a list of sections works fine. The template code for that would look something like:
{% for section in sections %}
{{ section.title }}
{% endfor %}
NOTE! The code above works just fine! But as soon as I add 'sections' as a list of dictionaries and have to reference the slug by item.section.slug the pages stop rendering.
Please advise.
Try using tuples:
View:
context['sections'] = [(section, tuple(get_subsections(section))) for section in sections]
Template:
{% for section, subsections in sections %}
<a class="sections" href="{% url 'sections:generate' section.slug %}">{{ section.title }}</a>
{% for subsection in subsections %}
<p>{{ subsection.title }}</p>
{% endfor %}
{% endfor %}

Detect row difference (view or model)?

I would like to display a list of publications on my website; however, I would also like to diaplay a header stating the year for each set of publications published on that particular year.
So I would like for my end result to be like this (my reputation is 1 :( I could not upload the image):
https://dl.dropboxusercontent.com/u/10752936/Screen%20Shot%202013-06-21%20at%206.00.15%20PM.png
I have a table with three columns; id (primary key), title (the title of the article), and date (the date of publications)
In my template file; doing the following will print the header before every article:
{% for curr_pub in all_publications %}
<h1>{{ curr_pub.date.year }}</h1>
<li>{{ curr_pub.title }}</li>
{% endfor %}
I am passing all_publications ordered by '-date' which means that I can compare the year of the current row curr_pub with the previous one and check if it differs or not; and print (or not print) the header accordingly. It seems however, that I cannot do that in the template.
Since I am new to Django and Python, I wasn't sure what to do and this is where I need help; my thoughts were the following:
1) Add a function in the model (def is_it_first_publication(self):) that returns true or false - but I really wasn't able to do that :| - ...and I'm not sure if that is what I needed to do or not!
2) Second one is to do in in the view, and pass extra variable(s) to the template; here's an example (which works just fine for this case):
In the view:
def publications(request):
all_publications = Publications.objects.order_by('-date')
after_first_row_flag = False
f_year = 'Null'
list_of_ids_of_first_publications = []
for curr_pub in all_publications:
if after_first_row_flag:
if curr_pub.date.year != f_year:
list_of_ids_of_first_publications.append(curr_pub.id)
f_year = curr_pub.date.year
else:
# The year of first (or earliest) publication has to be added
#
list_of_ids_of_first_publications.append(curr_pub.id)
f_year = curr_pub.date.year
after_first_row_flag = True
template = loader.get_template('counters/publications.html')
context = RequestContext(request, {
'all_publications': all_publications,
'list_of_first_publications': list_of_ids_of_first_publications,
})
return HttpResponse(template.render(context))
In the template:
{% for curr_pub in all_publications %}
{% if curr_pub.id in list_of_first_publications %}
<h1> {{ curr_pub.date.year }} </h1>
{% endif %}
<li> Placeholder for [curr_pub.title] </li>
{% endfor %}
The regroup built in filter can do this for you without annotating your objects in the view. As the documentation says, it's kind of complicated.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup
{% regroup all_publications by date.year as year_list %}
{% for year in year_list %}
<h1>{{ year.grouper }}</h1>
{% for publication in year.list %}
<li>{{ publication.title }}</li>
{% endfor %}
{% endfor %}
I think you want the regroup template tag;
{% regroup all_publications by date as publication_groups %}
<ul>
{% for publication_group in publication_groups %}
<li>{{ publication_group.grouper }}
<ul>
{% for publication in publication_group.list %}
<li>{{ publication.title }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
Maybe the template tag regroup could help.
Alternatively, you could do this grouping by year in the view function (will try to provide code later).