41👍
In Django 1.2, you can just do
{% if var in the_list %}
as you would in Python.
Otherwise yes, you will need a custom filter – it’s a three-liner though:
@register.filter
def is_in(var, obj):
return var in obj
6👍
Want to pass a comma separated string from the template? Create a custom templatetag:
from django import template
register = template.Library()
@register.filter
def in_list(value, the_list):
value = str(value)
return value in the_list.split(',')
You can then call it like this:
{% if 'a'|in_list:'a,b,c,d,1,2,3' %}Yah!{% endif %}
It also works with variables:
{% if variable|in_list:'a,b,c,d,1,2,3' %}Yah!{% endif %}
- [Django]-Determine the requested content type?
- [Django]-Django unit testing for form edit
- [Django]-Get_or_create throws Integrity Error
-3👍
In Django 3…
It’s simply by:
someHtmlPage.html
<html>
{%if v.0%}
<p>My string {{v}}</p>
{%else%}
<p>My another typy {{v}}</p>
{%endif%}
</html>
- [Django]-How can I chain Django's "in" and "iexact" queryset field lookups?
- [Django]-How can I find the union of two Django querysets?
- [Django]-How do I keep my Django server running even after I close my ssh session?
Source:stackexchange.com