Customised handling for wrong passwords entered in Login page in Django.

71 views
Skip to first unread message

Arun S

unread,
May 3, 2016, 6:01:59 AM5/3/16
to Django users
Hi,

I was trying to add a Customised View to Handle Wrong Passwords entered by Users.

As of now, Djangos Framework just outputs, "Invalid Username or Password" when a Wrong password is entered by the User.

This doesnt allow much flexibility if some information such as Invalid Access needs to be Logged.
Thus it would be good to Redirect these into a Customized view where the Logging of such information can be done and then redirected back to the Login page with Error.

Any Ideas on how easily this can be done ?

I tried adding a View when a Wrong password is called but always ended up in the below Error:

  File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py", line 578, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py", line 495, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))
NoReverseMatch: Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []


The login.html looks as simple as this.
td>{{ form.password }}
                                {% if form.password.errors %}
                                      <div id="password_error_div" class="error">You must specify a password.</div>
                                  {% endif %}
                                  {% if form.non_field_errors %}
                                  <div id="password_error_div" class="error">Invalid username or password.</div>
                                  {% endif %}

                          </td>
In the Highlighted case, i would like to handle the Error, Log the information to a file required and return back to the page.

Any Help on how this can be done would be great.
Thanks.

Simon Charette

unread,
May 3, 2016, 10:03:45 AM5/3/16
to Django users
Hi Arun,

If you only want to log failed login attempts I suggest you connect a receiver to
the user_login_failed signal[1] instead.

Cheers,
Simon

[1] https://docs.djangoproject.com/en/1.9/ref/contrib/auth/#django.contrib.auth.signals.user_login_failed

Arun S

unread,
May 4, 2016, 1:06:15 AM5/4/16
to Django users
Thanks Simon,

I could now do all kinds of Session logging by handling the signals.
Great Information.

Cheers
Arun.
Reply all
Reply to author
Forward
0 new messages