181👍
✅
Like Mihai and karelv have noted, this works:
{% if 'blabla' in item %}
...
{% endif %}
I get a 'dict object' has no attribute 'blabla'
if I use {% if item.blabla %}
and item
does not contain a blabla
key
41👍
You can test for key definition this way:
{% if settings.property is defined %}
#...
{% endif %}
- [Django]-How to show processing animation / spinner during ajax request?
- [Django]-Check permission inside a template in Django
- [Django]-What is a NoReverseMatch error, and how do I fix it?
15👍
This works fine doesn’t work in cases involving dictionaries. In those cases, please see the answer by tshalif.
Otherwise, with SaltStack (for example), you will get this error:
Unable to manage file: Jinja variable 'dict object' has no attribute '[attributeName]'
if you use this approach:
{% if settings.myProperty %}
note:
Will also skip, if settings.myProperty
exists, but is evaluated as False
(e.g. settings.myProperty = 0
).
- [Django]-Django render_to_string missing information
- [Django]-How to add custom search box in Django-admin?
- [Django]-Redirect to Next after login in Django
1👍
Actually, in the style of Python, if you do a simple if statement, it will work:
{% if settings.foo %}
Setting Foo: {{ settings.foo }}
{% endif %}
{% if settings.bar %}
Setting Bar: {{ settings.bar }}
{% endif %}
{% if settings.hello %}
Setting Hello: {{ settings.hello }}
{% endif %}
Output:
Setting Foo: baz
Setting Hello: world
Cheers!
- [Django]-Django can' t load Module 'debug_toolbar': No module named 'debug_toolbar'
- [Django]-What does error mean? : "Forbidden (Referer checking failed – no Referer.):"
- [Django]-Cancel an already executing task with Celery?
Source:stackexchange.com