我创建了两种不同类型的用户——使用 Django 的卡车和公司。这是我的用户注册页面的注册页面
注册后,用户是卡车还是公司的数据将进入数据库。
在我的登录页面中,只需要输入电子邮件 ID 和密码。
我想知道具有唯一 EmailID 的用户如何根据用户类型重定向到有效页面。
我创建了两种不同类型的用户——使用 Django 的卡车和公司。这是我的用户注册页面的注册页面
注册后,用户是卡车还是公司的数据将进入数据库。
在我的登录页面中,只需要输入电子邮件 ID 和密码。
我想知道具有唯一 EmailID 的用户如何根据用户类型重定向到有效页面。
你需要这样的东西;
def user_login(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
form.clean()
login(request, form.user_cache)
if form.user_cache.type == 'truck':
return HttpResponseRedirect('/some/where')
elif form.user_cache.type == 'company':
return HttpResponseRedirect('/some/where/else')
else:
form = AuthenticationForm()
return render(request, 'login.html', {'form' : form})
您自定义的用户模型可能是这样的:
class myuser(models.Model):
myUser = models.ForeignKey(User,..)
userType = models.CharField(choices=(('truck','truck'),('company','company'))
你的视图应该是这样的:
def login(request):
# authentication and getting the data from POST request to your login page,
# i assume you have a variable called user and you have your user object in it
userType = user.userType
if userType == company:
return HttpResponseRedirect('/some/url/')
if userType == truck:
return HttpResponseRedirect('/some/other/url/')