Django template string comparison failed - python

{{ 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

Related

Call filter by string name in Jinja

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?

Forloop.counter in Django

{% 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

How to change value of variable in django template?

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)

How to tell list from non-list in Django template?

I've got a Django template I'd sometimes like to pass a list and sometimes like to pass a single value. How can the template tell which it was given?
I'm thinking the value would be set like one of these:
context = {
'foo' : 'bar
}
or:
context = {
'foo' : ['bar', 'bat', 'baz']
}
Then, the template would have code that looks something like this:
{% if foo isa list %}
{% for item in foo %}
{{ item }}<br>
{% endfor %}
{% else %}
{{ item}}<br>
{% endif %}
I can set it up to have foo or foolist, for example, and check for one or the other. However, it'd be a bit nicer (imo) to just have foo that was either a list or not.
If you intend to do it this way then just add a check that it doesn't have format(in case of string) method and has 0 index, if so then its a list else considered single value
{% if foo.0 and not foo.format %}
{% for item in foo %}
{{ item }}<br>
{% endfor %}
{% else %}
{{ item}}<br>
{% endif %}
I think your approach is needlessly complicated.
I would just go with a list:
views.py
foo_list = ['bar']
context = {
'foo': foo_list,
'foo_len': len(foo_list),
}
template
{% if foo_len == 1 %}
{{ foo.0 }}
{% else %}
{% for item in foo %}
{{ item }}
{% endfor %}
{% endif %}

Django templates: False vs. None

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".

Categories