我创建了两种不同类型的用户 - 卡车和公司。这是我的用户注册页面
注册后,有关用户是卡车还是公司的数据将进入数据库。
只需输入电子邮件和密码。在我的自定义用户创建表单中,我添加了字段 username = email。
当我尝试使用有效凭据登录时,该页面不会根据用户类型将我重定向到特定页面。相反,我在 login.html 中为无效凭据创建的错误正在引发 - “您的电子邮件和密码不匹配。请重试。”
这是我的代码:
视图.py:
def login_view(request):
title = "Login"
if request.method == 'POST':
form = LoginForm(data=request.POST)
email = request.POST.get('email', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=email, password=password)
if form.is_valid():
auth.login(request, user)
user_type = form.cleaned_data['Label']
if user.is_active & user_type == 'Truck':
return HttpResponseRedirect('/post_load/')
elif user_type == 'Company':
return HttpResponseRedirect('/live_deal/')
else:
form = LoginForm()
return render(request, 'registration/login.html', {'form' : form, 'title': title})
Run Code Online (Sandbox Code Playgroud)
网址.py:
# url(r'^login/$', views.login_view),
# url(r'^accounts/login/$', views.login_view), …Run Code Online (Sandbox Code Playgroud) django django-authentication django-login django-custom-user