167π
β
Try removing the extra {{...}}
tags and the "..."
quotes around request.build_absolute_uri
, it worked for me.
Since you are already within an {% if %}
tag, there is no need to surround request.build_absolute_uri
with {{...}}
tags.
{% if 'index.html' in request.build_absolute_uri %}
hello
{% else %}
bye
{% endif %}
Because of the quotes you are literally searching the string "{{ request.build_absolute_uri }}"
and not the evaluated Django tag you intended.
π€Farmer Joe
19π
Maybe too late but here is a lightweight version :
{{ 'hello 'if 'index.html' in request.build_absolute_uri else 'bye' }}
This can be tested with Jinja:
>>> from jinja2 import Template
>>> t = Template("{{ 'hello 'if 'index.html' in request.build_absolute_uri else 'bye' }}")
>>> request = {}
>>> request['build_absolute_uri']='...index.html...'
>>> t.render(request=request)
'hello '
>>> request['build_absolute_uri']='something else...'
>>> t.render(request=request)
'bye'
>>>
π€chenchuk
- [Django]-Reference list item by index within Django template?
- [Django]-How to access the local Django webserver from outside world
- [Django]-Django-allauth social account connect to existing account on login
3π
I am adding the negative option of "not contains":
{% if 'index.html' not in request.build_absolute_uri %}
hello
{% else %}
bye
{% endif %}
And:
{{ 'hello 'if 'index.html' not in request.build_absolute_uri else 'bye' }}
- [Django]-Django TemplateSyntaxError β 'staticfiles' is not a registered tag library
- [Django]-Django storages: Import Error β no module named storages
- [Django]-How to reset db in Django? I get a command 'reset' not found error
1π
adding another option that might be useful (thatβs what I was looking for)
case-insensitive string contains (with lower filter)
{% if 'index.html' in request.build_absolute_uri|lower %}
hello
{% else %}
bye
{% endif %}
- [Django]-Django set field value after a form is initialized
- [Django]-Sending images using Http Post
- [Django]-How to format time in django-rest-framework's serializer?
Source:stackexchange.com