How can I distinguish between None and False in django templates?
{% if x %}
True
{% else %}
None and False - how can I split this case?
{% endif %}
Every Django template context contains True, False and None. For Django 1.10 and later, you can do the following:
{% if x %}
True
{% elif x is None %}
None
{% else %}
False (or empty string, empty list etc)
{% endif %}
Django 1.9 and earlier do not support the is operator in the if tag. Most of the time, it is ok to use {% if x == None %} instead.
{% if x %}
True
{% elif x == None %}
None
{% else %}
False (or empty string, empty list etc)
{% endif %}
With Django 1.4 and earlier, you do not have access to True, False and None in the template context, You can use the yesno filter instead.
In the view:
x = True
y = False
z = None
In the template:
{{ x|yesno:"true,false,none" }}
{{ y|yesno:"true,false,none" }}
{{ z|yesno:"true,false,none" }}
Result:
true
false
none
You can create a custom filter:
#register.filter
def is_not_None(val):
return val is not None
then use it:
{% if x|is_not_None %}
{% if x %}
True
{% else %}
False
{% endif %}
{% else %}
None
{% endif %}
Of course, you could tweak the filter to test whatever condition you like also...
An enhancement of the previous answers could be:
{% if x|yesno:"2,1," %}
# will enter here if x is True or False, but not None
{% else %}
# will enter only if x is None
{% endif %}
Create context_processor (or use from the django-misc module misc.context_processors.useful_constants) with True, False, None constants and use {% if x == None %} or {% if x == False %}
context_processor.py:
def useful_constants(request):
return {'True': True, 'False': False, 'None': None}
Here is another tricky way:
{% if not x.denominator %}
None
{% else %}
{% if x %}
True
{% else %}
False
{% endif %}
{% endif %}
That's because "None" doesn't have the attribute "denominator", while it is 1 for both "True" and "False".
Related
I have iterations with some filters:
{% if filter_name == None %}
{{ value }}
{% elif filter_name == 'trim' %}
{{ value|trim }}
{% elif filter_name == 'title' %}
{{ value|title }}
{% endif %}
Is there a way to call the filter by string name?
Some like this value|call_filter('filter') ?
def call_filter(value: str, name: str):
return load_templating.env.filters[name](value)
This work fine, but what do you think of this solution?
{% for each_item in item.artifacts %}
{% if each_item.scanner_count > 0 and each_item.scanner_match > 0 %}
{% if forloop.counter <= 5 %}
<tr>
<td>{{each_item.threat_name}}</td>
</tr>
{% else %}
{% if forloop.last %}
<p><b><i> {{ forloop.counter|add:"-5" }} rows were truncated. See full report for more details. </i></b></p>
{% endif %}
{% endif %}
{% else forloop.counter -=1 %}
{% endif %}
{% endfor %}
ERROR:Malformed template tag at line 171: "else forloop.counter -=1"
I want to increment the counter only when if condition is successful. Dont know how to do it with forloop.counter. Goal is to print 5 rows of valid output(scanner count >0 and Scanner match >0)
You can use combination of add and forloop counter to achieve what you want to achieve. But remember you need to pass some variable (I've used rank here for kinda storing the increment variable in template.
views.py
rank = 0
return render(request, "base.html", {"items": items, "rank": rank})
html
{% for each_item in items %}
{% if each_item.scanner_count > 0 and each_item.scanner_match > 0 %}
{% if forloop.counter|add:rank <= 5 %}
<tr><td>{{each_item.threat_name}}</td></tr>
<br>
{% else %}
{% if forloop.last %}
<p><b><i> {{ forloop.counter|add:"-5" }} rows were truncated. See full report for more details. </i></b></p>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
As other commenters have noted, you cannot make an assignment of the forloop.counter in the template, as that would be considered logic that should be in the view (controller). Remove the {% else forloop.counter -= 1 %} from your code and it should work as I think you intended. If not, either add logic in the object (context) being passed to the template or user other forloop attributes/variables as found in the Django documentation here: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#for
I want to declare a flag variable in django template and the change it if some thing happened.
But when I change value of variable by custom tag it is declared a new variable and doesn't change.
for example my template tag and django template is:
template tag:
#register.simple_tag
def update_variable(value):
return value
html:
{% with True as flag %}
<h1>1: {{ flag }}</h1>
{% for e in events %}
{% if e.title == '***' %}
{% update_variable False as flag %}
<h1>2: {{ flag }}</h1>
{% endif %}
{% endfor %}
<h1>3: {{ flag }}</h1>
{% endwith %}
and result is:
1: True
2: False
3: True
But the end result should be False! How to do this?
I find a solution for it. for check all element of list we can use custom filter an do some thing there:
html:
//load custom filter
{% load my_filters %}
{% if "anything we want"|is_in:events.all %}
//do some thing...
{% else %}
//do some thing...
{% end if%}
custom filter in my_filter file:
register = template.Library()
def is_in(case, list):
for element in list:
if element.time_span == case:
return True
return False
register.filter(is_in)
{{ is_true }}
{% if is_true == "True" %}
<h2>Say True</h2>
{% else %}
<h2> False </h2>
{% endif %}
But instead it went to else clause even though {{ is_true }} returns True
Any idea?
def some_func():
if ....:
return True
else:
return False
You don't need to use "True" in your template:
{% if is_true == True %}
Or just:
{% if is_true %}
If you use "True" in your template then you are comparing the boolean True with the string "True" (which are not the same) and end up in the else clause of your template.
In other words you would be doing:
{% if True == "True" %}
<h2>Say True</h2>
{% else %} # You will end up here
<h2> False </h2>
{% endif %}
You can find more info about Django's Template Language in the documentation
I want to change the value of the variable declared outside the loop within a loop. But always changing, it keeps the initial value outside the loop.
{% set foo = False %}
{% for item in items %}
{% set foo = True %}
{% if foo %} Ok(1)! {% endif %}
{% endfor %}
{% if foo %} Ok(2)! {% endif %}
This renders:
Ok(1)!
So the only (bad) solution have found so far was this:
{% set foo = [] %}
{% for item in items %}
{% if foo.append(True) %} {% endif %}
{% if foo %} Ok(1)! {% endif %}
{% endfor %}
{% if foo %} Ok(2)! {% endif %}
This renders:
Ok(1)!
Ok(2)!
But, its is very ugly! Is there another more elegant solution?
Try also dictionary-based approach. It seems to be less ugly.
{% set vars = {'foo': False} %}
{% for item in items %}
{% if vars.update({'foo': True}) %} {% endif %}
{% if vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if vars.foo %} Ok(2)! {% endif %}
This also renders:
Ok(1)!
Ok(2)!
as mentioned in the documentation:
Please note that assignments in loops will be cleared at the end of
the iteration and cannot outlive the loop scope.
but as of version 2.10 you can use namespaces:
{% set ns = namespace(foo=false) %}
{% for item in items %}
{% set ns.foo = True %}
{% if ns.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if ns.foo %} Ok(2)! {% endif %}
You could do this to clean up the template code
{% for item in items %}
{{ set_foo_is_true(local_vars) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_foo_is_true(local_vars):
local_vars['foo'] = True
return ''
env.globals['set_foo_is_true'] = set_foo_is_true
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)
This could be generalized to the following
{{ set_local_var(local_vars, "foo", False) }}
{% for item in items %}
{{ set_local_var(local_vars, "foo", True) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_local_var(local_vars, name, value):
local_vars[name] = value
return ''
env.globals['set_local_var'] = set_local_var
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)