alias51
alias51

Reputation: 8618

How to handle an else function in a nested if statement

I have a nested if statement in a template:;

{% if object in request.user.mymodel_set.all %}
    {% if object.pk == request.session.field_pk %}
        Selected
    {% else %}
        <form method="POST" action="myURL" class="">
            {% csrf_token %}
            <input type="submit" value="Select">
        </form>
    {% endif %}
{% endif %}

However the <input> does not render. It does when the parent {% if %} statement is removed. Am I missing something as to how the else statement is handled in this template?

Upvotes: 1

Views: 50

Answers (1)

Sina Farhadi
Sina Farhadi

Reputation: 785

Try This

{% if object in request.user.mymodel_set.all %}
   {% if object.pk == request.session.field_pk %}
    Selected
   {% elif object.pk != request.session.field_pk %}
    <form method="POST" action="myURL" class="">
        {% csrf_token %}
        <input type="submit" value="Select">
    </form>
   {% endif %}
{% endif %}

if this is not work too maybe the variables you check in your conditions not True

Upvotes: 4

Related Questions