Django-axis不能使用自定义登录视图 [英] Django-axes not working with custom login view
问题描述
在提出这个问题之前,我已经遵循以下链接,因为它似乎是一个重复的,但没有用。所以我再问一次。
django-axis不捕获失败的登录尝试,但捕获管理失败的尝试很好
django-axis与管理站点一起工作,但无法从用户自定义登录视图中捕获失败的尝试。我在'/ project / app / views.py'上的自定义视图如下所示:
from axis.decorators import watch_login
@watch_login
def user_login(request):
if request.method =='POST':
username = request.POST.get('username' )
password = request.POST.get('password')
...
和 settings.py 中,我使用的中间件类是
axis.middleware.FailedLoginMiddleware'
,其他django-axis配置如下:
AXES_LOGIN_FAILURE_LIMIT = 3
AXES_COOLOFF_TIME = 30
AXES_LOCKOUT_TEMPLATE ='/templates/app/login.html'
和我的 urls.py 如下:
from axes.decorators import watch_login
pre>
urlpatterns = patterns( '',
url(r'^ login / $',watch_login(user_login),{'template_name':'app / login.html'}),
当我尝试访问管理页面或用户页面时,我收到以下错误:
<
/ admin /
中的NameError
名称'user_login'未定义
我甚至尝试将中间件类从'axis.middleware.FailedLoginMiddleware'更改为'axes.middleware.FailedAdminLoginMiddleware',如下所示链接,但现在没有任何工作,因为它显示错误
发生服务器错误。请联系管理员。
我想我提出了问题。
任何帮助是赞赏。
谢谢解决方案一年后,但我希望问题和答案对别人有用。我刚刚使用自定义登录视图来完成这个操作,并且和你完全相同,但是django 1.9.6和django-ax 1.6。我没有在
watch_login()
中包含user_login
url,因为如果视图被调用,逻辑上它将被
它工作正常 - 我以几个不同的用户和轴的身份登录创建了访问日志,我可以在管理界面中查看。
您的
/ admin /
中的NameError似乎表明错误不在django-axis,但与您的urls.py - 特别是登录行(因为该视图在访问管理员或用户登录页面时被调用,对吗?)。您是否从应用导入视图导入应用的视图,即?然后,您需要确保
user_login
视图的命名空间是显式的:url(r'^ login / $',watch_login(views.user_login),{'template_name':'app / login.html'}),...
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-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
解决方案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 inwatch_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 theuser_login
view's namespace is explicit:url(r'^login/$', watch_login(views.user_login), {'template_name': 'app/login.html'}),...
这篇关于Django-axis不能使用自定义登录视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!