Home:ALL Converter>Django login with django-axes

Django login with django-axes

Ask Time:2014-09-10T15:50:18         Author:Lee

Json Formatter

I created a site with django. Users should be able to login. The login-view looks like this:

from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
....
if request.method == 'POST':       
        username = request.POST['username']#get username
        password = request.POST['txtPwd']# and password 
        user = authenticate(username=username, password=password) #checking username and pwd
        if user is not None:
            if user.is_active:
                login(request, user)

But with this "solution" i can't handle an brute force attack. So I looked around and found this: Throttling brute force login attacks in Django

The first answer was helpful. I choosed django-axes because django-ratelimit count only the amout of calling a view.

But here is my problem: When i try to login with wrong password it doesn't count the failure. (Only at the /admin-section).

I found no option to "add" my login-view to django-axes.

So here is my question:

How can I configure django-axes to handle the failed logins from my login-view?

EDIT: Here is my settings-file:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'axes',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'axes.middleware.FailedLoginMiddleware'
)

...

AXES_LOCK_OUT_AT_FAILURE = False
AXES_USE_USER_AGENT = True
AXES_COOLOFF_TIME = 1
AXES_LOGIN_FAILURE_LIMIT = 50

Author:Lee,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25760023/django-login-with-django-axes
Alex Lisovoy :

By default django-axes used django's login view *(django.contrib.auth.views.login). In middleware this view decorate with watch_login. \n\nSo you can solve your issue in two ways:\n\n\nuse standard login view. In this way django-axes does not require additional setup.\ndecorate your's login view with watch_login decorator. \n\n\nFor example: views.py\n\nfrom axes.decorators import watch_login\n...\n\n@watch_login\ndef your_custom_login_view(request):\n ...\n\n\nIt will then be used like this in class based view as mentioned by @Ali Faizan:\n\n@method_decorator(watch_login, name='dispatch')\nclass your_custom_login_view():\n ...\n",
2014-09-11T07:41:12
yy