I'm having a problem in a jinja template. If I have an object 'jsonobj' like so:
{
"type": "test",
"people": [{"name": "bill", "age": 43}]
}
in my template I'm trying to do an if like
{% if 'test' is jsonobj.type %}
<!-- some html here -->
{% else %}
<!-- some other html here -->
{% endif %}
but I get an error:
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endfor'. You probably made a nesting mistake. Jinja is expecting this tag, but currently looking for 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.
when I add curlies around jsonobj.type I get
jinja2.exceptions.TemplateSyntaxError: expected token 'name', got '{'
Was looking at the docs here and I thought it should work...any ideas?
thanks
I tried any variation I could think of; ==, is, isin and so on. Tried wrapping json obj in one and two sets of curlies. Did lots of googling
May be you forget endif tag at end?
{% if 'test' is jsonobj.type %}
...
{% else %}
...
{% endif %}
{% endif %} it seems you forgot to turn off the if blog
I was able to resolve this issue by making sure the whitespace/indenting was even. That seemed to resolve the issue
Related
Im new to Django and I don't really have a clear idea how the Django Template language works but I have a dictionary that is something like this:
{
"info":{
"a":"test1",
"b":"test2",
}
}
How can I get the values for "a" and "b" in Django template.
I'm not sure if that makes sense but what I mean would look something like this in Python:
for i in info:
print(info.i)
I already tried this but it didn't work
{%for i in info %}
{% if info.i %}
console.log('{{info.i}}')
{% endif %}
{% endear %}
I have a jinja code for python and its giving me an error it doesn't give me in python
{% for i, juice in enumerate(a['juice'] for a in television):};
alert({{ juice }});
{% endfor %};
The Error I'm getting is
expected token ',', got 'for'
You don't need to add : at the end of the for statements in Jinja2. And, you are not properly closing the tag - missing the % before the }.
Plus, there is no enumerate() function in Jinja2, use the loop.index0:
{% for a in television %}
{{ loop.index0 }}, {{ a["juice"] }}
{% endfor %}
If you want to use more Python in the templates, you should probably look at the Mako engine.
Here is a snippet of code in a template that uses a custom templatetag I wrote that does work:
{% if theme == 'books_literature' %}
{{ theme|prettify:'/' }}
{% else %}
{{ theme|prettify:' ' }}
{% endif %}
PyCharm is complaining about the colon and the characters after the colon, even though they are valid...
Here is an image showing what it looks like:
The hover I get says I should add closing curly braces... Which makes sense, if it wasn't expecting an argument.
#thebjorn reported it as PY-14890 ( https://youtrack.jetbrains.com/issue/PY-14890 )
How can I insert this:
<strong>{% trans "Error!" %}</strong> {% trans "blablabla." %}
Instead of "xxx" here:
{% include "alert.html" with connotation="error" message="xxx" %}
Please advise.
As far as i know, you can't do that unless you create the complete message in view and pass it as a template context parameter.
Instead of passing complete message i would suggest to pass some error code and based on that show the appropriate message in "alert.html".
So your include statement will change to something like this:
{% include "alert.html" with connotation="error" error_code=err_code %}
and inside alert.html, you can put conditions to show appropriate message.
{% if error_code == "404" %}
<strong>{% trans "Not Found!" %}</strong>
{% endif %}
I want to define a variable if a condition is met:
{% if order_item.status.ordering >= 60 and is_client %}
{% with readonly=1 %}
{% else %}
{% with readonly=0 %}
{% endif %}
...some code
{% endwith %}
However, I get the following error:
Invalid block tag: 'else', expected 'endwith'
How can I fix this bug in django?
Perhaps it's better to add a readonly property/method to your order_item.status.
Alternatively you could also try a templatefilter or templatetag.
This code doesn't make any sense, first you should learn the with syntax. You have opened it twice and closed once and you don't have any useful code inside with.