How to change value of variable in django template? - python

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)

Related

string in list of list

I have two lists like:
DevInDB = ['Dev1','Dev2','Dev3']
and
DevInMesh = [['Dev1',0,0],['Dev2',1,1]]
I want to know if, for any DevInDB string, there is the same string in DevInMesh.
I wrote this code that works (in python)
for dev in DevInDB:
if any(dev in DevMesh for DevMesh in DevInMesh ):
print(dev)
else:
print('no')
I try to ust this code in a HTML file using jinja, but the 'any' function doesn't work. How can I do it?
You need to compute this before sending it to jinja.
Ex:
DevInDB = ['Dev1','Dev2','Dev3']
DevInMesh = [['Dev1',0,0],['Dev2',1,1]]
DevInMesh = {k for k, *_ in DevInMesh}
DevInDB = [(i, i in DevInMesh) for i in DevInDB]
print(DevInDB)
I've done it this way:
{% for DevDB in DevInDB %}
{% set ns = namespace(foo=false) %}
{% for DevMesh in DevInMesh %}
{% if DevMesh [0] == DevDB %}
{% set ns.foo = True %}
{% endif %}
{% endfor %}
and later on:
{% if ns.foo %}
{% else %}
{% endif %}
You can replace the any expression with the simple list comprehension where non-empty list will be True:
[1 for DevMesh in DevInMesh if dev in DevMesh]
Or better is to remove the computational part from template because a template is the view part that works on presentation level of the context, such that all pre-computed expressions should be stored in the context and only printed in the template.

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 model query tune up

I want to reduce the number of template filter calls in my_filter_function(). Because It's used inside two for-loops inside a template. Please see below for my code setup.
class ModelA(models.model):
models.ForeignKey(OtherModel1)
class ModelB(models.model):
models.ForeignKey(OtherModel2)
class ModelC(models.Model):
a = models.ForeignKey(ModelA)
b = models.ForeignKey(ModelB)
def my_views(request):
return render(request, 'my_template.html', {
'a_list': ModelA.objects.all(),
'b_list': ModelB.objects.all(),
})
and in my template, I have
{% for a in a_list %}
{% for b in b_list %}
{% with b|my_filter_function:a as my_val %}
Val: {{my_val}}
{% endwith %}
{% endfor %}
{% endfor %}
the above template will will call the my_filter_function filter function, I need to find another way to reduce the number of my_filter_function function calls, because the filter function is accessing the DBs several thousand times per template now.
#register.filter
def my_filter_function:(b, a):
z = ModelC.objects.filter(a=a, b=b)
if z.count() > 0:
return "OK"
else:
return "Not OK"
Here's a faster alternative.
Fetch all the ids of A and B in C at once:
z = ModelC.objects.values_list('a_id', 'b_id')
a_related, b_related = zip(*z) # split into a and b ids
Pass these to your context:
def my_views(request):
return render(request, 'my_template.html', {
'a_list': ModelA.objects.all(),
'b_list': ModelB.objects.all(),
'a_related': a_related,
'b_related': b_related,
})
And then use if...in in your template. The custom template filter can now be discarded:
{% for a in a_list %}
{% for b in b_list %}
{% if a.id in a_related and b.id in b_related %}
"OK"
{% else %}
"Not ok"
{% endif %}
{% endfor %}
{% endfor %}
That replaces all the multiple queries in your filter with just one.

Django template string comparison failed

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

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