Django-axes not working with custom login view

2019-06-11 15:13发布

问题:

I have followed the below links before asking this question as it seems like a duplicate, but of no use. So I'm asking again.

Django login with django-axes

django-axes not capturing failed login attempt, but captures admin failed attempts fine

The django-axes works fine with the admin site, but it is unable to capture the failed attempts from the user custom login view. My custom view at '/project/app/views.py' is as follows:

from axes.decorators import watch_login
@watch_login
def user_login(request):
     if request.method == 'POST':
         username = request.POST.get('username')
         password = request.POST.get('password')
         ......

and in settings.py, the middleware class I'm using is

'axes.middleware.FailedLoginMiddleware'

and the other django-axes configuration is as follows:

AXES_LOGIN_FAILURE_LIMIT = 3 AXES_COOLOFF_TIME = 30 AXES_LOCKOUT_TEMPLATE = '/templates/app/login.html'

and my urls.py is as follows:

from axes.decorators import watch_login

urlpatterns = patterns('',
url(r'^login/$', watch_login(user_login), {'template_name': 'app/login.html'}),

when I try to access the admin page or the user page, I'm getting the following error:

NameError at /admin/

name 'user_login' is not defined

I even tried changing the middleware class from 'axes.middleware.FailedLoginMiddleware' to 'axes.middleware.FailedAdminLoginMiddleware' as suggested in this link, but now nothing seems to work as it showing the error

A server error occurred. Please contact the administrator.

I think I made the question clear.

Any help is appreciated. Thanks

回答1:

A year late, but I hope the question and answer are useful to someone else. I just tried this out with my custom login view, and have pretty much the same setup as you, but django 1.9.6 and django-axes 1.6. I did not wrap the user_login url in watch_login() because if the view's getting called, logically it's getting logged by the @watch_login decorator.

It worked fine - I logged in as several different users and axes created access logs that I could view in the admin interface.

Your NameError at /admin/ seems to indicate that the error is not with django-axes but with your urls.py - specifically the login line (because that view is getting called when access the admin or user login pages, right?). Did you import your app's views, i.e. from app import views? And then you need to ensure the user_login view's namespace is explicit:

url(r'^login/$', watch_login(views.user_login), {'template_name': 'app/login.html'}),...