How to use if condition in Django inside {% block content %} - python

I am trying to apply if condition inside {% block content %} by using the following code:
{% for item in execution_log %}
{% if item.description == 'Set On' %}
#Condition
{% elseif item.description == 'Set Off' %}
#Condition
{% elseif item.description == 'FFMPEG error' %}
#Condition
{% endif %}
{% endfor %}
But I am not getting any output. Is the syntax for the if condition correct?

You should use elif instead of elseif

Hint: before the {% endif %}, add these lines and test again. This should drive you to the solution, since you didn't show the code that pushes the execution_log in the template context.
{% else %}
#UNKNOWN {{ item.description }}

Related

If else condition error in django template

I write a simple django condition but it not working
{% if request.user == "sami" %}
sami
{% else %}
khan
{% endif %}
Requset.user is an object.
You need to write
{% if request.user.username == "sami" %}
or
{% if request.user.name == "sami" %} whatever is in your model

declare variable in if else block in django framework

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

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

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