如何在django中登录后为不同的用户重定向不同类型的页面
how to redirect different types of pages for different users after login in django
我使用 Django 创建了两种不同类型的用户 - 卡车和公司。这是我的用户注册页面 Registration Page
注册后,用户是卡车还是公司的数据会进入数据库。
在我的登录页面,只需要输入EmailID和密码。
我想知道具有唯一 EmailID 的用户如何根据用户类型重定向到有效页面。
我假设,身份验证和从 POST 请求(在 views.py 中)获取数据到我的登录页面工作不正常。请有人帮我解决这个问题。
这是我的代码:
views.py:
@csrf_protect
def login_view(request):
title = "Login"
if request.method == 'POST':
email = request.POST.get('email', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=email, password=password)
form = LoginForm(data=request.POST)
if form.is_valid():
user_type = form.cleaned_data['Label']
if 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})
urls.py:
url(r'^login/$', 'django.contrib.auth.views.login', {'authentication_form': LoginForm}),
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'authentication_form': LoginForm}),
forms.py:
class LoginForm(auth.forms.AuthenticationForm):
email = forms.EmailField(label=_("Email"),widget=forms.EmailInput)
CHOICES= (('Truck', 'Truck'),('Company', 'Company'),)
Label = forms.ChoiceField(choices=CHOICES, label='Label', widget=forms.RadioSelect())
login.html:
{%extends "registration/header.html"%}
{% block content %}
{% if form.errors %}
<p>Your email and password didn't match. Please try again.</p>
{% endif %}
<form class="form-horizontal" method="post" action = "." >{%csrf_token%}
<div class="panel panel-default login">
<div class="panel-heading">
<span class="glyphicon glyphicon-lock"></span> Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<div class='col-sm-6 col-sm-offset-4'>
<table border="0">
<div class="col-sm-4">
<tr><th><label for="id_user" class="col-sm-4 control-label">Email:</label></th><td>{{ form.email }}</td></tr> </div>
<div class="col-sm-4">
<tr><th><label for="id_password" class="col-sm-4 control-label">Password:</label></th><td>{{ form.password }}</td></tr> </div>
</table> </div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<div class="checkbox">
<label>
<input type="checkbox"/>
Remember me
</label>
</div>
</div>
</div>
<div class="form-group last">
<div class="col-sm-offset-4 col-sm-8">
<button type="submit" class="btn btn-success btn-sm">
Sign in</button>
<input type="hidden" name="next" value="/" />
<label class="col-sm-offset-3">
<a href="#">Forget Password? </a>
</label>
</div>
</div>
</form>
</div>
<div class="panel-footer">
Not Registered? <a href="/register/">Register</a></div>
</div>
</form>
{% endblock %}
这里有一个更好的方法来检查您视图中的无线电场:-
user_type = form.cleaned_data['Label']
if user_type == 'truck':
# send the user to truck page
else:
# send the user to company page
还有一些事情——一旦你使用 is_valid() 进行验证,你真的不需要调用那个 clean() 方法,你应该将那个 authenticate() 东西移到 POST部分。
我使用 Django 创建了两种不同类型的用户 - 卡车和公司。这是我的用户注册页面 Registration Page
注册后,用户是卡车还是公司的数据会进入数据库。
在我的登录页面,
我想知道具有唯一 EmailID 的用户如何根据用户类型重定向到有效页面。
我假设,身份验证和从 POST 请求(在 views.py 中)获取数据到我的登录页面工作不正常。请有人帮我解决这个问题。
这是我的代码:
views.py:
@csrf_protect
def login_view(request):
title = "Login"
if request.method == 'POST':
email = request.POST.get('email', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=email, password=password)
form = LoginForm(data=request.POST)
if form.is_valid():
user_type = form.cleaned_data['Label']
if 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})
urls.py:
url(r'^login/$', 'django.contrib.auth.views.login', {'authentication_form': LoginForm}),
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'authentication_form': LoginForm}),
forms.py:
class LoginForm(auth.forms.AuthenticationForm):
email = forms.EmailField(label=_("Email"),widget=forms.EmailInput)
CHOICES= (('Truck', 'Truck'),('Company', 'Company'),)
Label = forms.ChoiceField(choices=CHOICES, label='Label', widget=forms.RadioSelect())
login.html:
{%extends "registration/header.html"%}
{% block content %}
{% if form.errors %}
<p>Your email and password didn't match. Please try again.</p>
{% endif %}
<form class="form-horizontal" method="post" action = "." >{%csrf_token%}
<div class="panel panel-default login">
<div class="panel-heading">
<span class="glyphicon glyphicon-lock"></span> Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<div class='col-sm-6 col-sm-offset-4'>
<table border="0">
<div class="col-sm-4">
<tr><th><label for="id_user" class="col-sm-4 control-label">Email:</label></th><td>{{ form.email }}</td></tr> </div>
<div class="col-sm-4">
<tr><th><label for="id_password" class="col-sm-4 control-label">Password:</label></th><td>{{ form.password }}</td></tr> </div>
</table> </div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<div class="checkbox">
<label>
<input type="checkbox"/>
Remember me
</label>
</div>
</div>
</div>
<div class="form-group last">
<div class="col-sm-offset-4 col-sm-8">
<button type="submit" class="btn btn-success btn-sm">
Sign in</button>
<input type="hidden" name="next" value="/" />
<label class="col-sm-offset-3">
<a href="#">Forget Password? </a>
</label>
</div>
</div>
</form>
</div>
<div class="panel-footer">
Not Registered? <a href="/register/">Register</a></div>
</div>
</form>
{% endblock %}
这里有一个更好的方法来检查您视图中的无线电场:-
user_type = form.cleaned_data['Label']
if user_type == 'truck':
# send the user to truck page
else:
# send the user to company page
还有一些事情——一旦你使用 is_valid() 进行验证,你真的不需要调用那个 clean() 方法,你应该将那个 authenticate() 东西移到 POST部分。