I'd like to get the value of an element by key within an if statement.
Example:
works:
{{ example[5] }}
doesn't work:
{% if example2 is example[5] %} something ... {% endif %}
Exception thrown:
Unexpected token "punctuation" of value "[" ("end of statement block" expected)
Thank you very much
Kind regards
Instead of
{% if example2 is example[5] %} something ... {% endif %}
try changing 'is' to '=='
{% if example2 == example[5] %} something ... {% endif %}
Maybe you should use the attribute function to get the object or array value. This link may help you
Related
I am trying to assign value to a dictionary in Jinja2 and its not working properly and showing error.
expected token 'end of statement block', got '='
My Code:
{% set sequence = ['a1', 'b1']%}
{% set dic = {} %}
{% for filter in search_result.filters %}
{% for seq_key in sequence %}
{% if seq_key == filter.key %}
{# here i wish to create a dictionary where key= seq_key and value = filter_object#}
{% do dic[seq_key]=filter %}
{% endif %}
{% endfor %}
{% endfor %}
The do statement takes only an expression, not a statement like an assignment. Since Jinja2 does not directly support assigning to a dict by key, you can call the dict's update method with a singleton dict containing the new key-value mapping instead.
Change:
{% do dic[seq_key]=filter %}
to:
{% do dic.update({seq_key: filter}) %}
I need to have a nested loop in my Django template, where the outer loop goes through a list of objects, and the inner loop goes through a list of those object id's, and I want to only do something for the id's on the inner list, it never executes however. I think it has something to do with the condition for the if statement, because if I replace it with a true statement it works but it doesn't work as it is now
(I have checked to see that the id's overlap)
{% for outer in outer_obj_list %}
{% for inner_id in inner_id_list %}
{% if outer.id == inner_id %}
// do something
console.log({{inner_id}});
console.log({{outer.id}});
{% endif %}
{% endfor %}
{% endfor %}
Syntax seems correct. I would just verbosely output everything.
Perhaps it should be something like this:
{% for main_obj in main_obj_list %}
main_obj: {{ main_obj }}
{% for obj_id in obj_id_list %}
obj_id: {{ obj_id}}
main_obj: {{ main_obj.id}}
{% if main_obj.id == obj_id %}
// do something
match: {{main_obj.id}} == {{obj_id}} ;
{% endif %}
{% endfor %}
{% endfor %}
Brother I am also face this problem so my clever mind get some clever solution about this problem we can do that with JavaScript easily so we need to run it in JavaScript and then.
{% for outer in outer_obj_list %}
{% for inner_id in inner_id_list %}
if(outer.id == inner_id.id ){
console.log({{inner_id.id}});
console.log({{outer.id}});
//And also if we reserve place in DOM then we can
//change the inner Html of them easily like.
//demo = document.getElementById("demo");
//demo.innerHTML = inner_Id.id or outer.id
}
{% endfor %}
{% endfor %}
I think the best way to describe is problem is with an example.
{% for content in contents %}
{% for stuff in {{content}} %}
{{stuff}}
{% endfor %}
{% endfor %}
I am using google app engine webapp templates. I can't seem to use a result from the parent forloop {{content}} as a variable for its child forloop. TemplateSyntaxError: Could not parse the remainder: '{{content}}' from '{{content}}' Is it possible to do this? Thanks!!
You can use only content without braces around:
{% for content in contents %}
{% for stuff in content %}
{{ stuff }}
{% endfor %}
{% endfor %}
When you are inside the first for-loop, content exists in the context, as any other variable. Same thing for stuff in the inner loop. Plus, blocks are generally using argument as variables, except in it is surrounded by quotes.
The {{ }} notation can be use to only display the variable in the document.
I have something like the following:
{% for i in "xxxxxxxxx" %}
...
{% if passedInList.*forloop.counter0|add:"1"* %} Do Something {% endif %}
...
{% endfor %}
Obviously the if statement is incorrect. I have been using this method to create index names in forms for quite some time, but have never had to use the for loop counter index in an if statement.
Is this possible? If so, how would I go about doing so?
Thanks!
Try to use a with statement to save the forloop counter in a context variable, like so:
{% with index=forloop.counter %}
{% if passedInList.index %} do something {% endif %}
{% endwith %}
(Also, seems like you can use forloop.counter instead of forloop.counter0|add:"1")
In my Django template as I am iterating through a list of objects, I'd like to have one list item say:
<li>Blah</li>
and then another do:
<li>Blah</li>
I see value|random as an option but for some reason this doesn't work:
{% ifequal [1, 2]|random 1 %}
{{ post.title }}
{% else %}
{{ post.title }}
{% endifequal %}
Doing this throws this error:
u'ifequal' takes two arguments
Is there any way to accomplish this? I would think it should be simple but I realize the Django templating language doesn't allow for variable assignments.
Thanks!
You can't put a list directly into the template like that, make_list is what you're after.
make_list returns a list of strings, so this would work.
{% if 12|make_list|random == '1' %}
<li>Blah</li>
{% else %}
<li>Blah</li>
{% endif %}