station
station

Reputation: 7145

Modify django templates to check for values in a dict

I have the following Django template.

{% load custom_tags %}
<ul>
{% for key, value in value.items %}
<li> {{ key }}: {{ value }}</li>
{% endfor %}

I need to check for the value and do some modifications.

If the value is True , instead of value I have to print Applied , else if it False I need to print Not Applied.

How to achieve that?

Upvotes: 0

Views: 60

Answers (1)

rfj001
rfj001

Reputation: 8498

Very simple if-else clause here. Take a look at the django template docs to familiarize yourself with some of the common tags.

{% if value %}
  APPLIED
{% else %}
   NOT APPLIED 
{% endif %}

You asked how to do this as a filter... I'm not sure why, but here is it:

In your app's templatetags directory create a file called my_tags.py or something and make the contents

from django import template

register = template.Library()

@register.filter
def applied(value):
    if value:
        return 'Applied'
    else:
        return 'Not applied'

Then in your template make sure to have {% load my_tags %} and use the filter with {{ value|applied }}

Upvotes: 1

Related Questions