0

Using Django's built-in LoginView, I want to display an error message on incorrect login. This works fine. However, upon refreshing of the login page, the error message persists.

I want to remove this error message when the user refreshes the page.

urls.py

from django.urls import path
from django.contrib.auth.views import LoginView

urlpatterns = [
    path('login/', LoginView.as_view(template_name='login.html'), name="login"),
]

login.html

{% if form.non_field_errors %}
  <div class="alert alert-danger" role="alert">
    {% for error in form.non_field_errors %}
      <p>{{ error }}</p>
    {% endfor %}
  </div>
{% endif %}

<form method="post" action="{% url 'accounts:login' %}">
            {% csrf_token %}
            <p><input type="text" name="username" autofocus="" required="" id="id_username" placeholder="username"></p>

            <p><input type="password" name="password" required="" id="id_password" placeholder="password"></p>

            <button type="submit" value="Login">Login</button>
            <a id="signup-btn" href="{% url 'accounts:signup' %}"">Don't have an account? Sign up</a>
 </form>

The error message being printed: 'Please enter a correct username and password. Note that both fields may be case-sensitive.'

Any help would be greatly appreciated.

Edit: Sorry for any confusion, I have updated the question to clarify things.

Edwarric
  • 513
  • 2
  • 9
  • 19
  • 1
    Which error is showing? Is it the one at row 4? Also, can you please print the errors the page outputs? – Johan Apr 01 '19 at 15:12
  • I've updated my question – Edwarric Apr 01 '19 at 15:15
  • 1
    How else are you displaying the page other than "visiting through a url link"? – Daniel Roseman Apr 01 '19 at 15:26
  • @Edwarric I was wondering about that part too. You need to make a greater effort in describing when and how the undesired event happen. Is it when you post to the page? How do you get to the page in the first place? – Johan Apr 01 '19 at 15:29
  • Displaying the page: through a link only. But on refresh the error messages stay. – Edwarric Apr 01 '19 at 15:35
  • @Johan Honestly, I've been working on this bug for so long simply by refreshing the page I didn't think to try visiting it from a link. Sorry again for the confusion, I will re-update my question to clarify things. – Edwarric Apr 01 '19 at 15:36
  • @Edwarric It appears that your form gets to experience postback You should be able to solve this with a redirect (even if it's to the same view) after submitting, but you'll probably need to implement or extend the current login view if you want to solve this. You can check more from [this answer](https://stackoverflow.com/a/5823683/10400050) – Johan Apr 01 '19 at 18:36

1 Answers1

0

I used your own code, what I did was add a button with the data-dispats attribute to close the bootstrap alert when required, the complete element looked like this:

{% if form.non_field_errors %}
   <div class="alert alert-danger" role="alert">
  <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        {% for error in form.non_field_errors %}
             <p>{{ error }}</p>
        {% endfor %}
      </div>
 {% endif %}