declare variable in if else block in django framework - python

I am doing a conditional block in loop django template , but unable to find exact answer for this.
Please consider my code here,
{% for data in app_data %}
{% if forloop.counter == 1 %}
{% declare_some_variable = 'hello' %}
{% else %}
{% declare_some_variable = 'bye' %}
{% endif %}
{{ declare_some_variable }} {{ data.name }}
{% endfor %}
This is what I want. But it does not work.

Try to use the with tag.
{% with declare_some_variable = "hello" %}
{% endwith %}
You can read more about with here
Or you can simply do:
{% trans "hello" as declare_some_variable %}

Related

'if' statement in jinja2 template

I'm trying to write an if statement in jinja template:
{% for key in data %}
{% if key is 'priority' %}
<p>('Priority: ' + str(data[key])</p>
{% endif %}
{% endfor %}
the statement I'm trying to translate in Python is:
if key == priority:
print(print('Priority: ' + str(data[key]))
This is the error i'm getting:
TemplateSyntaxError: expected token 'name', got 'string'
Why the loop?
You could simply do this:
{% if 'priority' in data %}
<p>Priority: {{ data['priority'] }}</p>
{% endif %}
When you were originally doing your string comparison, you should have used == instead.
We need to remember that the {% endif %} comes after the {% else %}.
So this is an example:
{% if someTest %}
<p> Something is True </p>
{% else %}
<p> Something is False </p>
{% endif %}

access first message in Django template

I am using the following code to access the first message in Django template,
{% if messages %}
{% for message in messages %}
{% if forloop.first %}
{{ message }}
{% endif %}
{% endfor %}
{% endif %}
How I can achieve the same without using the for loop in a single statement.
Following should work:
{% if messages %}
{{ messages.0 }}
{% endif %}

Django {% blocktrans %}: How to handle pluralization inside a for loop?

I have the following loop in my Django template:
{% for item in state.list %}
<div> HTML (CUSTOMERS BY STATE) </div>
<!-- print sum of customers at bottom of list -->
{% if forloop.last %}
<h4>{{ forloop.counter }} Valued Customers</h4>
{% endif %}
{% endfor %}
Obviously, if I end up with only one customer, I'd like to print singular "Valued Customer"
According to Django's docs, one should use blocktrans. Tried the following, a few flavors of nesting:
{% blocktrans count %}
{% if forloop.last %}
<h4>
{{ forloop.counter }}
Valued Customer
{% plural %}
Valued Customers
</h4>
{% endif %}
{% endblocktrans %}
Keep getting TemplateSyntaxError: Invalid block tag: 'blocktrans', expected 'empty' or 'endfor'
Is there no way to combine with another loop? Any ideas how to solve? Thanks!
Here is the working code, thanks to alko:
{% load i18n %}
<!-- ... -->
{% if forloop.last %}
<h4>
{{ forloop.counter }}
{% blocktrans count count=forloop.counter %}
Valued Customer
{% plural %}
Valued Customers
{% endblocktrans %}
</h4>
{% endif %}
Probably, you forgot to load translation tags. Add following line at the top of your template:
{% load i18n %}
After you fix that, note that for a blocktrans tag after count a variable, whose value will serve for plural detection, should be specified, so you probably need something like
{% blocktrans count count=forloop.counter %}
To pluralize use this:
Customer{{ forloop.counter|pluralize }}

Django if statement not working

{% extends "base.html" %}
{% block content %}
<h1>{{ page }}</h1>
{% for category in categories %}
{% if category.page == page %}
<h2>{{ category.title }}!</h2>
{% for item in categoryitems %}
{{ category.title }} {{ item.category }}
{% if item.category == category.title %}
<h3>{{ item.title }}</h3>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endblock %}
The first statement if category.page == page is working fine, but the other one - if item.category == category.title - doesn't, for the sake of checking if everything is fine I've also added those {{category.title}} and {{item.category}} outputs and they are identical, so why doesn't the loop show me my item.title ? Without the if, it works, but, of course, shows every element in the list, which is smthn I don't need.
Do not depend on the page output to tell you what is correct, since the output is dependent on the code. Instead, do it correctly in the first place.
{% if item.category == category %}

How to use a for loop inside a Django conditional?

Basically if there is a certain GET parameter in the url (in this case "latest") I want to slice the object list by a different number than the usual. But doing this:
{% if 'latest' in request.GET %}
{% for object in object_list|slice:"22" %}
{% else %}
{% for object in object_list|slice:"10" %}
{% endif %}
// blah blah
{% endfor %}
causes a syntax error since Django expects a closing endfor instead of the else. Is there any way to use for loops inside conditionals?
You need to have a body in your for loop.
{% if 'latest' in request.GET %}
{% for object in object_list|slice:"22" %} {{ object }} {% endfor %}
{% else %}
{% for object in object_list|slice:"10" %} {{ object }} {% endfor %}
{% endif %}
Without it, you're saying the equivalent of the following Python code:
if 'latest' in request.GET:
for object in slice(object_list, 22):
#No code here
else:
for object in slice(object_list, 10):
#No code here
which obviously is an error.
Just close the for loop inside each conditional:
{% if 'latest' in request.GET %}
{% for object in object_list|slice:"22" %}
{{ object.name }}
{% endfor %}
{% else %}
{% for object in object_list|slice:"10" %}
{{ object.name }}
{% endfor %}
{% endif %}

Categories

Resources