django: user is never authenticated even when username and password is
correct
Okay, so my auth_view is this (the view where I authenticate the login /
check if the username matches the password).
def auth_view(request):
username = request.POST['username']
password = request.POST['password']
if username is not None and password is not None: #making sure
username and password is not empty
user = authenticate(username=username, password=password)
else:
return redirect('/loggedin_invalid_view/')
if user is not None:
auth.login(request, user)
return redirect('/loggedin_view/')
else:
return redirect('/loggedin_invalid_view/')
and my login form in the template is this:
<form method="post" action="/auth_view/">{% csrf_token %}
<label for="username">Username:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="Login">
</form>
Now, my model where I have the username and password saved in is called
Users and I created a form from it called UsersForm. In the form, it has a
SlugField called 'username' 'password'. The template in which the Users
form is:
<form method="post" action="">{% csrf_token %}
{{ form.first_name }} <br>
{{ form.username }} {{ form.password }} <br>
<input type="submit" value="Register"/>
</form>
So I registered (the UsersForm is basically the registration form) with
the username as 'user1' and the password as 'password1' and then when I
try signing in, it redirects to /loggedin_inavlid_view rather than to
loggedin_view, how come? Do I have to edit my view to manually go into the
Users model and check if there is a user with the username called user1
and then check if user1's password is password1?
No comments:
Post a Comment