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}) %}
Related
{% for cred in allcreds %}
{% if cred.datasource.name == '...' %}
<h4>{{ cred.datasource }}</h4>
{% endif %}
{% endfor %}
In this case I check the datasource name in the conditional. Then it prints out the datasource. I want to have the datasource in the conditional.
I assume if you print out {{ cred.protocoldatasource }} it will output nothing, because your "relation" protocoldatasource does not exist.
The available foreign-keys that your model ProtocolUserCredentials has are: protocol, data_source, user, protocol_user.
So if you do
{% if cred.data_source.name == 'Demonstration Protocol, ...' %}
or any other of the mentioned relations you can access your related models.
Also note that {% if foo = 'bar' %} is invalid, you'll need to have == in the if-statement.
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 have a problem when I was using django template:
{% for key in wordList %}
<li><a href="#" data-weight={% wordList[key] %} >{{ key }}</a></li>
{% endfor %}
Django would say: Invalid block tag: 'wordList[key]', expected 'empty' or 'endfor'. Here the wordList is a dictionary, for example: {'Fish':19, 'Chips':5 }
How can I insert django block in the html sentence? I want them to be like:
<li>Fish</li>
<li>Chips</li>
How to do with the double quotation " " marks?
Thank you very much.
Iterate directly over the key/value pairs:
{% for key, value in wordList.items %}
<li>{{ key }}</li>
{% endfor %}
You can do dictionary-style lookups with dot syntax in a template, but only when you know the key rather than having it in an template variable.
I am trying to get the values of keys in dictionary in jinja.
{% for key, values in chat_data.items %}
{{values.from}} <br/>
<b> Query: </b>{{values.init_query}}<br/>
{% for k in values.chat %}
{% set last_response = '111' %}
{% endfor %}
{{last_response}}
{% endfor%}
Error : Invalid block tag: 'set', expected 'empty' or 'endfor'
How can I set the variable in Jinja
You cannot use last_response outside for loop.
You can use the Jinja2 built-in last() filter instead of for with set
{{ values.chat | last }}
Why not use the Jinja2 built-in last() filter?
http://jinja.pocoo.org/docs/templates/#last
i.e. try:
{% for key, values in chat_data.items %}
{{values.from}} <br/>
<b> Query: </b>{{values.init_query}}<br/>
{{ values.chat|last }}
{% endfor%}
Using set should still work though.
I'm using django's template system, and I'm having the following problem:
I pass a dictionary object, example_dictionary, to the template:
example_dictionary = {key1 : [value11,value12]}
and I want to do the following:
{% for key in example_dictionary %}
// stuff here (1)
{% for value in example_dictionary.key %}
// more stuff here (2)
{% endfor %}
{% endfor %}
However, this does not enter on the second for loop.
Indeed, if I put
{{ key }}
on the (1), it shows the correct key, however,
{{ example_dictionary.key }}
shows nothing.
In this answer, someone proposed using
{% for key, value in example_dictionary.items %}
However, this does not work in this case because I want (1) to have information regarding the particular key.
How do I achieve this? Am I missing something?
I supose that you are looking for a nested loop. In external loop you do something with dictionary key and, in nested loop, you iterate over iterable dictionary value, a list in your case.
In this case, this is the control flow that you need:
{% for key, value_list in example_dictionary.items %}
# stuff here (1)
{% for value in value_list %}
# more stuff here (2)
{% endfor %}
{% endfor %}
A sample:
#view to template ctx:
example_dictionary = {'a' : [1,2]}
#template:
{% for key, value_list in example_dictionary.items %}
The key is {{key}}
{% for value in value_list %}
The key is {{key}} and the value is {{value}}
{% endfor %}
{% endfor %}
Results will be:
The key is a
The key is a and the value is 1
The key is a and the value is 2
If this is not that you are looking for, please, use a sample to ilustrate your needs.