title: Issue with displaying alerts after failed logins
body: <p>I am implementing a website as my final project and I want to display a message for users that failed to log in more than 3 times prompting them to reset their passwords.</p>
  <p>I have already tried different solutions I found online, but nothing is helping.</p>
  <p>I am writing the backend in Django, but Django-axes also didn't work for me, so I am trying JS now.</p>
  <pre><code>&lt;script type=&quot;text/javascript&quot;&gt;
      var login_attempts = 3;
      function checkAttempts()
      {
          var failed_alert = document.querySelectorAll(&quot;a&quot;)[0].textContent;
  
          if (failed_alert === &quot; Invalid username or password &quot;) {
              login_attempts--;
              console.log(login_attempts);
              alert('failed login')
  
              if (login_attempts==0)
              {
                  alert(&quot;Do you want to reset your password?&quot;);
              }
          } else {
              console.log(login_attempts);
              return false;
          }
      }
  &lt;/script&gt; 
  </code></pre>
  <p>This is the example I found on Stack Overflow. I think I am close to solving this problem, I am getting messages, but when printing &quot;login_attemps&quot; in a console at first I get 3 and then continuously 2, no matter how many times I write incorrect password.</p>
  <p>The function is called in form. I also tried to do it in body onload=&quot;&quot;</p>
  <pre><code>&lt;form id=&quot;login-form&quot; class=&quot;form&quot; action=&quot;login&quot; method=&quot;post&quot; onsubmit=&quot;checkAttempts();&quot;&gt;
  </code></pre>
  <p>The whole authentication is implemented in views.py django:</p>
  <pre><code>def login(request):
      if request.user.is_authenticated:
          return redirect('/')
      else:
          if request.method == 'POST':
              username = request.POST['username']
              password = request.POST['password']
  
              user = auth.authenticate(username=username, password=password)
  
              if user is not None:
                  auth.login(request, user)
                  return HttpResponseRedirect(request.GET.get('next', LOGIN_REDIRECT_URL))
              else:
                  messages.info(request, 'Invalid username or password')
                  return redirect('login.html')
          else:
              return render(request, 'login.html')
  </code></pre>
  <p>I feel like I am losing my ideas. I was trying to solve this for 2 weeks with no effects. If anyone has any idea, I would be thankful. It can be both in js or django.</p>