我正在尝试将我创建的自定义身份验证表单传递给auth.views.login .
我找到的所有教程都是通过urls.py中的url()和登录视图的url来完成的,例如:
url(r'^login/$', auth.views.login,{'authentication_form':MyAuthenticationForm})
但是 I want the url to be the same as the index url ,如果用户通过身份验证,则显示索引,否则使用我的自定义身份验证表单显示登录表单 .
这是我的 views.py :
from django.shortcuts import render
from django.contrib.auth import views as auth_views
def cp(request):
if request.user.is_authenticated():
return render(request, 'index.html')
# How to pass my custom authentication form ?
return auth_views.login(request)
这是有效的,但是如何告诉django我的自定义身份验证表单?
1 回答
我建议您将
/login/
保留为登录URL,但使用login_required装饰器作为索引视图 . 当新用户访问您的索引URL时,他们将被重定向到您的登录页面,然后在他们登录后返回到索引URL .这种方法在Django中非常典型,并且比使用索引url处理登录更简单 . 如果您真的想从索引页面调用登录视图,那么您应该使用与url模式中相同的kwarg
authentication_form
: