0

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?

3
  • Are you asking about the >= comparison? Please update your question to clarify what your problem is.
    – S.Lott
    Feb 16, 2010 at 21:07
  • Oops, I was checking if the word "1" appears in the string.
    – TIMEX
    Feb 16, 2010 at 21:08
  • Please update the question. Please don't add comments. It's your question, please try to make it clear for others to follow.
    – S.Lott
    Feb 16, 2010 at 21:19

3 Answers 3

3

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.

2
  • 3
    Relational and containment operators in {% if %} are only in the development version. Feb 16, 2010 at 21:10
  • 1
    True -- it's only scheduled for release (as 1.2) in 3 weeks. Feb 16, 2010 at 23:44
3

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

Example:

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.