I write simple Django project and I want to store client IP and date whenever a login attempt fails. I prepare model:
class FailIp(models.Model):
ip = models.CharField(
max_length=16,
verbose_name=_(u'fail login ip'),
)
date = models.DateTimeField(default=datetime.now)
But I do not know how to hook up to the login error. Urls:
url(
r'^login/$',
'django.contrib.auth.views.login', {
'template_name': 'login.html'
},
name='login'
),
How to do it?
This is a very good use case for django.contrib.auth.signals.user_login_failed
signal - was introduced in Django 1.5, it is sent when the user failed to login successfully:
from django.contrib.auth import signals
def listener_login_failed(sender, credentials, **kwargs):
# handle log in failure
signals.user_login_failed.connect(listener_login_failed)
Also see tests for django.contrib.auth.signals
.