0

I have this code:

{% if username_is_ok == True %}
    <input type="text" name="inputName" placeholder="Name" required>
{% else %}
    <div class="control-group error">
        <input type="text" name="inputName" placeholder="Name" value="{{ username }}"
               class="text-error" required>
        <span class="help-block text-error">User already exists</span>
    </div>
{% endif %}

And debugger says that username_is_ok == True equals {bool} True. But it goes to the else branch and when page loads I see the error message. I can't see no mistakes and I feel frustrated about that. Can you help me? Thanks.

5
  • try using {% if username_is_ok %} Commented May 8, 2013 at 2:24
  • I've added == True in order to doublecheck value of username_is_ok, so this is not a solution. Commented May 8, 2013 at 2:28
  • Did you check that username_is_ok equals True?? Commented May 8, 2013 at 2:31
  • @nnaelle he said he did in his question. Commented May 8, 2013 at 2:33
  • Oh i thought that he just checked if it was a bool. my bad then. I would check in the views anyways, just in case. Sorry i misunderstood Commented May 8, 2013 at 2:34

1 Answer 1

4

The Django templating language is not Python but rather its own custom "templating language" with minimal logic (this is by design: it's encouraged that you keep "controller" logic away from templates). Therefore, you'll find that some valid Python may not be valid template syntax

In your example, the templating language tries to evaluate "True" as a literal. Since you probably didn't define a key named "True" in the context, a VariableDoesNotExist exception is raised, and the template treats the value as the python None type.

The debugger evaluates the conditional as Python, not Django templating language, and so the value is tested against the Python True and evaluates to True

In order for the logic to work, you might want to try adding True to your template context. Something like: {"True": True}

related: https://stackoverflow.com/a/5672415/927229

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.