Home > Blockchain >  How check empty variable in template? Exception Value: Could not parse the remainder
How check empty variable in template? Exception Value: Could not parse the remainder

Time:04-01

{% extends 'pygiustizia/base.html' %}
{% block body %}
<div >
    <h2>Login</h2>
    <p>Prego, inserisci le tue credenziali di login.</p>

    <div class={% if len(tmplVar.loginErr) > 0  %} "alert alert-danger" {% endifequal %}> {% if tmplVar.loginErr is not None %} {{tmplVar.loginErr}} {% endif %}</div>
    <form action="{% url 'login' %}" method="post">
        <div >
            <label>Username</label>
            <input type="text" name="username"  value="{{tmplVar.username}}">
            <span >{% if tmplVar.usernameErr is not None %} {{tmplVar.usernameErr}} {% endif %}</span>
        </div>
        <div >
            <label>Password</label>
            <input type="password" name="password"  value="{{tmplVar.password}}">
            <span >{% if tmplVar.passwordErr is not None %} {{tmplVar.passwordErr}} {% endif %}</span>
        </div>
        <div >
            <input type="submit"  value="Login">
        </div>
    </form>
</div> 
{% endblock %}

Exception Value: Could not parse the remainder: '(tmplVar.loginErr)' from 'len(tmplVar.loginErr)'

What I miss? How can I check if variable is empty or lenght 0?

CodePudding user response:

You can not use len(…): function calls are deliberately restricted in Django's template language. You can make use of the |length template filter [Django-doc]. But here checking the truthiness is enough:

<div {% if tmplVar.loginErr %}{% endif %}>

this will check if the length is greater than zero, since collections (lists, tuples, strings, etc.) have truthiness False if these are empty.

  • Related