bmeyer71
bmeyer71

Reputation: 372

template tag called in if statement

I am wondering if there is any way that I can call a template tag from within an if statement.

I am trying to do the following.

{% for k,v in form.amenities.field.choices %}
    {% if {% check_hidden k %} %}
        {{ v }}
    {% endif %}
{% endfor %}

The {% check_hidden k %} tag returns True or False from the table for the item being looked up. I can confirm that {% check_hidden k %} returns either True or False on it's own, but I am wondering if I can wrap an if statement around it?

The app that I am working with is inherited, so I am trying to minimize the amount of changes needing to be done for now. Currently it is just being rendered as {{form.amenities}}, but I have added an additional field to the model that I now need to check before displaying the field in the template.

If there is another way that this could be done, I am open to suggestions.

Thanks in advance.

Upvotes: 4

Views: 2977

Answers (2)

S.Lott
S.Lott

Reputation: 391818

Whatever it is that check_hidden does for a choice value needs to be fixed in a fundamental way.

If the field choices are dynamic, the correct set of choices needs to be established in the view function, removing the need to do check_hidden in the template at all.

https://stackoverflow.com/search?q=%5Bdjango%5D+dynamic+choices

Will give you lots of ways to have dynamic choices. Many of these are done in the view function, removing the need for this kind of if-statement in the template.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798506

It sounds like check_hidden was written as a template tag when what is needed is a filter.

{% if k|check_hidden_filter %}

Upvotes: 6

Related Questions