Using https://docs.djangoproject.com/en/3.2/topics/auth/default/#how-to-log-a-user-in, I've tried to authenticate users on my website. However, the authenticate method always returns a None
type. I am not sure why because the user does exist in my system and I am typing the password correctly.
In my system, I have it so that the username is the email so that it matches with the login authentication.
Here is the code where I check for validation:
if request.method == 'POST':
if form.is_valid():
email=request.POST['email']
password=request.POST['password']
user2 = User.objects.get(username=email)
print(user2.first_name)
print(user2.username)
user = authenticate(username=email, password=password)
if user is not None:
login(user)
return HttpResponseRedirect(reverse('index'))
else:
form.clean()
else:
print("form is invalid")
In my case, the form
is valid and user2 DOES exist in the database as I have checked it's email and username and they are all the same. Why is this happening?
EDIT: Some more information. I've created the user by using a signup form. This user is stored in the database as I can see it inside the admin panel. This is what the login form looks like (not using google login)
Here is the html file incase it is needed:
{% extends "mainapp/base.html" %}
{% load socialaccount %}
{# Loads the css files and pics #}
{% load static %}
{% block content %}
<div class="container col">
<h3 class="text-center">Login</h3>
<form action="" method="post">
{% csrf_token %}
<div class="form-group">
<label>Email: </label>
<input name="{{ form.email.name }}" class="form-control" type="email" required>
{{ form.email.errors }}
</div>
<div class="form-group">
<label>Password: </label>
<input name="{{ form.password.name }}" class="form-control" type="password" required>
{{ form.password.errors }}
</div>
{{ form.non_field_errors }}
<!-- Sign in button -->
<button type="submit" class="btn btn-primary col-md-12" value="sign_in">Sign in</button>
</form>
<!-- Google sign up button -->
<a href="{% provider_login_url 'google' %}">
<button class="btn btn-light col-md-12 mt-2">
<img width="20px" style="margin-bottom:3px; margin-right:5px" alt="Google sign-in"
src="{% static 'mainapp/google_logo.png' %}"/>
Sign in with Google
</button>
</a>
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…