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 %}
Related
I'm using spotify's API to get this week's new releases and I'm trying to parse through the returned data to display it on the webpage using jinja2, the problem is that every time i try to run the program an error pops up saying: jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'info'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.
I tried different syntax for appending to a list in jinja2 but nothing works it just gives me the same jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag
To better understand the data spotify returns you can use spotify's console: https://developer.spotify.com/console/get-new-releases/?country=&limit=&offset=
This is the code i wrote, help would be greatly appreciated. Thanks.
<section class="wrapper">
<div class="new-releases">
<h1 id="NRH">New Releases</h1>
<hr>
<div class="row">
<div class="records">
{% for record in releases['albums']['items'] %}
{% for key, value in record.items() %}
{% set info = [] %}
{% if key == "name" %}
{% append value to info %}
{% endif %}
{% if key == "artists" %}
{% for k, v in value[0].items() %}
{% if k == "name" %}
{% append v to info %}
{% endif %}
{% endfor %}
{% endif %}
{% if key == "external-urls" %}
{% for k, v in value[0].items() %}
{% if k == "spotify" %}
{% append v to info %}
{% endif %}
{% endfor %}
{% endif %}
{% if key == "images" %}
{% for k, v in value[1].items() %}
{% if k == "url" %}
<img src="{{v}}" title="{{info[0]}}.{{info[1]}}">
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</div>
</div>
</div>
<hr>
</section>
change {% append v to info %}
to {{ info.append(v) }}
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 %}
{% 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 %}
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 %}
I am already trying to concatenate like this:
{% for choice in choice_dict %}
{% if choice =='2' %}
{% with "mod"|add:forloop.counter|add:".html" as template %}
{% include template %}
{% endwith %}
{% endif %}
{% endfor %}
but for some reason I am only getting "mod.html" and not the forloop.counter number. Does anyone have any idea what is going on and what I can do to fix this issue? Thanks alot!
Your problem is that the forloop.counter is an integer and you are using the add template filter which will behave properly if you pass it all strings or all integers, but not a mix.
One way to work around this is:
{% for x in some_list %}
{% with y=forloop.counter|stringformat:"s" %}
{% with template="mod"|add:y|add:".html" %}
<p>{{ template }}</p>
{% endwith %}
{% endwith %}
{% endfor %}
which results in:
<p>mod1.html</p>
<p>mod2.html</p>
<p>mod3.html</p>
<p>mod4.html</p>
<p>mod5.html</p>
<p>mod6.html</p>
...
The second with tag is required because stringformat tag is implemented with an automatically prepended %. To get around this you can create a custom filter. I use something similar to this:
http://djangosnippets.org/snippets/393/
save the snipped as some_app/templatetags/some_name.py
from django import template
register = template.Library()
def format(value, arg):
"""
Alters default filter "stringformat" to not add the % at the front,
so the variable can be placed anywhere in the string.
"""
try:
if value:
return (unicode(arg)) % value
else:
return u''
except (ValueError, TypeError):
return u''
register.filter('format', format)
in template:
{% load some_name.py %}
{% for x in some_list %}
{% with template=forloop.counter|format:"mod%s.html" %}
<p>{{ template }}</p>
{% endwith %}
{% endfor %}
You probably don't want to do this in your templates, this seems more like a views job: (use of if within a for loop).
chosen_templates=[]
for choice in choice_dict:
if choice =='2':
{% with "mod"|add:forloop.counter|add:".html" as template %}
template_name = "mod%i.html" %index
chosen_templates.append(template_name)
Then pass chosen_templates to your template where you will have only
{% for template in chosen_templates %}
{% load template %}
{% endfor %}
Also, I don't quite understand why you are using a dict to select the template with a number that is not in the dictionnary. for key,value in dict.items() may be what you are looking for.
Try without using the block "with"
{% for choice in choice_dict %}
{% if choice =='2' %}
{% include "mod"|add:forloop.counter|add:".html" %}
{% endif %}
{% endfor %}