I have created two different types of users - truck & company using Django. Here is my registration page of a user Registration Page
After registering, the data about whether the user is a truck or company will go to the database.
In my login page, only EmailID and Password are to be entered.
I would like to know how a user with a unique EmailID can redirects to a valid page based on the type of user.
I assume, Authentication and getting the data from POST request(in views.py ) to my login page isn't working properly. Please, someone help me fixing this.
here's my code:
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 %}
Here's a better way to check that radio field in your views:-
user_type = form.cleaned_data['Label']
if user_type == 'truck':
# send the user to truck page
else:
# send the user to company page
A few more things - you don't really need to call that clean() method once you validate using is_valid() and you should move that authenticate() thing inside that POST section.