Home > Enterprise >  Placement of {% if form.errors %} on login template
Placement of {% if form.errors %} on login template

Time:07-16

I'm not sure if I'm allowed to ask these types of questions here since it's not a problem per say, so please let me know. But I was wondering for the login.html template on Django:

{% extends 'learning_logs/base.html' %}

{% block content %} 

    {% if form.errors %}
        <p>Your username and password didn't match. Please try again.</p>
    {% endif %}

    <form method = "post" action="{% url 'login' %}">
        {% csrf_token %}
        {{ form.as_p }}

        <button name = "submit">log in</button>
        <input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
    </form>

{% endblock content %}

How come it checks for the form.errors before the it even processes the form? Thanks for any help on this question.

CodePudding user response:

The template ordering doesn't define how the form logic works, only how certain items are displayed. You could put the {% if form.errors %} block after the <form> and there would be no difference to how the form is processed by Django. Furthermore, form.errors will only exist once a form has been validated with is_valid() and any errors exist.

  • Related