views:

34

answers:

2

How can i split cases for None and False in django templates.

{%if x %} True {%else%} None and False - how i can split this cases? {%endif%}

+1  A: 

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

Ned Batchelder
+1  A: 

You might be able to get rid of the if/else tag, and 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
Alasdair