开发者

django login not showing error message

开发者 https://www.devze.com 2023-04-01 22:18 出处:网络
I have a regular django login page but for some reason if I enter the wrong login information it\'s not showing the error messages and it just refreshing the page. Any ideas?

I have a regular django login page but for some reason if I enter the wrong login information it's not showing the error messages and it just refreshing the page. Any ideas?

login.html

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

        <form method="post" action=".">
            <p>
                <label for="id_username">Usernam开发者_如何学运维e:</label>
                {{ form.username }}
            </p>
            <p>
                <label for="id_password">Password:</label>
                {{ form.password }}
            </p>
            {% if next %}
                <input type="hidden" name="next" value="{{ next }}" />
            {% else %}
                <input type="hidden" name="next" value="/portal/" />
            {% endif %}
            <input type="submit" value="login" />
        </form>


It could be possible that your creating a new form, replacing the form which contains the form.errors and returning it to the template. If form.is_valid(): is not True then it looks for the next logical return. It may be possible that you are creating a new form right before you return to the template.

EDIT:

If you are using the django.contrib.auth.views.login then the documentation says that you should specify the action in the form and assign it to

{% url 'django.contrib.auth.views.login' %}.

You should change your form to

<form method="POST" action="{% url 'django.contrib.auth.views.login' %}"> also use the use the {% csrf_token %} after the <form>.


For those of you still wondering like me, thanks to Django login not showing errors , errors are raised in the general clean() method and are displayed in the template via {{ form.non_field_errors }}.

Try the below code in your form.

{% for error in form.non_field_errors %}
   {{error}}
{% endfor %}
0

精彩评论

暂无评论...
验证码 换一张
取 消