TIMEX
TIMEX

Reputation: 272014

IF in the Django template system

How do I do this:

{% if thestring %}

    {% if thestring.find("1") >= 0 %}

    {% endif %}

{% endif %}

I am assuming I need to build a template filter? Will that work?

Upvotes: 0

Views: 2137

Answers (3)

Alex Martelli
Alex Martelli

Reputation: 882023

You don't need to build a custom filter, though one would work -- the alternative of coding

{% if thestring %}

    {% if "1" in thestring %}

    {% endif %}

{% endif %}

would also go just fine.

Upvotes: 3

Jack M.
Jack M.

Reputation: 32080

I believe you'll find that the Django template system isn't designed to have complex logic in it. This type of processing should happen in your view, then be passed to the template.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798884

It would. But use the in operator instead of the find() method.

Example:

{% if thestring|contains:"1" %}

Upvotes: 3

Related Questions