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.
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}) %}
In a Django template, I want to construct dynamically urls
I try to concatenate (see below) but got an error :
Could not parse the remainder: '+key+'_index'' from ''ecrf:'+key+'_index''
{% for key, values in forms.items %}
<!-- for exemple a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}
urls
app_name='ecrf'
urlpatterns = [
path('inclusion/create/', views.InclusionCreate.as_view(), name='inclusion_create'),
expected result:
Inclusion
You can use the add template tag[Django docs] to add strings together:
{% for key, values in forms.items %}
<!-- for example a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}
But this feels a bit hacky to me, perhaps your forms dictionary can be restructured so that such logic is done in the view itself? For example the dictionary can be:
{'inclusion': {'value': <some_value>, 'create_url': 'ecrf:inclusion_create'}}
And in your template your loop would be:
{% for key, values in forms.items %}
<!-- for example a key would be 'inclusion' -->
{{ key|capfirst }}
{% endfor %}
I am trying to access the form based on the index value how can i do that exactly?
Ex:
{% for line in data_lines %}
{{line}}
{% with x=forloop.counter %}
{{form.x}}
{% endwith %}
{% endfor %}
As Django discourages adding too much logic hence I don't think it's possible using built-in Django Template features. You can achieve this by sending a dict instead of data_lines and accessing via key, value as:
{% for key, value in data_lines.items %}
{{value}}
{{ form.key }}
{% endfor %}
If you really want it then you could write a custom template filter.
Using Django template tags, I am trying to check if an object Boolean field that I passed to the template (using python) is True.
If I print the object on the page I see the value True/False:
<p>{{ obj.bool }}</p>
I've tried:
{% if {{ obj.bool }} == True %}
HELLO
{% endif %}
Which raises a syntax error
Could not parse the remainder: '{{' from '{{'
And:
{% if '{{ obj.bool }}' == 'True' %}
<p>HELLO</p>
{% endif %}
Gives me nothing...?
You dont need {{}} inside tag, just use:
{% if obj.bool %}
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.