I have issues with creating a login page in django. I am done with the views and html and stuff but the issue is that when I hit login it won't log the user in. After some debugging I saw that the issue is that the value of the user is None but I don't know why
If anyone could help me with this it would be great
i am posting the code below:
views:-
from django.shortcuts import render, redirect
from .forms import RegisterForm, ProfileForm
from django.contrib.auth.models import User
from django.contrib.auth import login, authenticate
def loginUser(request):
if request.method == 'POST':
email = request.POST['email']
password = request.POST['password']
try:
user = User.objects.get(email=email)
except:
print("User does not exist!")
user = authenticate(request, email=email, password=password)
if user is None:
login(request, user)
return redirect('home')
else:
print('The email or password is incorrect')
html:-
<div>
<div >
<div >
<h1 ><span>login</span></h1>
</div>
<div >
<div >
<span >welcome</span>
<form method="POST">
{% csrf_token %}
<div >
<div >
<label ><span>Email</span></label>
</div>
<input type="text" name="email" placeholder="placeholder" />
</div>
<div >
<div >
<label ><span>Password</span></label>
</div>
<div >
<div >
<input type="password" name="password" placeholder="placeholder" id="lPassword" />
</div>
<div >
<input type="checkbox" onclick="reveal3()" id="check3" />
<span >
<span>Show password</span>
</span>
</div>
</div>
</div>
<div >
<input type="submit" value="login" >
</div>
</form>
</div>
<div >
<a
href=""
target="_blank"
rel="noreferrer noopener"
>
Forgot password
</a>
<a href="{% url 'register' %}" >
I don't have an account
</a>
</div>
</div>
</div>
signals:-
import email
from django.db.models.signals import post_save, post_delete
from .models import UserInfo
from django.dispatch import receiver
from django.contrib.auth.models import User
def userUpdate(sender, instance, created, **kwargs):
if created:
user = instance
profile = UserInfo.objects.create(
user=user,
email=user.email,
first_name=user.username,
last_name=user.last_name,
)
def userDelete(sender, instance, **kwargs):
user = instance.user
user.delete()
post_save.connect(userUpdate, sender=User)
post_delete.connect(userDelete, sender=UserInfo)
models:-
from django.db import models
from django.contrib.auth.models import User
import uuid
class UserInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
email = models.EmailField(max_length=200)
password = models.CharField(max_length=200)
confirm_password = models.CharField(max_length=200)
profile_photo = models.ImageField(null=True, blank=True, upload_to='profile_photo/',
default='profile_photo/default.jpg')
phone_number = models.CharField(max_length=10)
address = models.TextField(null=True)
pin_code = models.CharField(null=True, max_length=6)
region = models.CharField(null=True, max_length=200)
state = models.CharField(null=True, max_length=100)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True,
editable=False)
def __str__(self):
name = self.first_name ' ' self.last_name
return str(name)
Well this is all of it and i think it might have been a bit much but never mind Please someone, if you would help me with this it would be a great help
Ohh right and the output is:- The email or password is incorrect (The current output)
CodePudding user response:
According to the doc, the authenticate
method doesn't require a request object
. Furthermore, I'd suggest using the username
in the parameter as well instead of the email
since you are using the django
User
model
.
authenticate(username='john', password='secret')
Also, you might want to tweak the code you have to kinda facilitate using the username
. For example, within the if request.method == 'POST':
block:
email = request.POST['email']
password = request.POST['password']
# You can import from django.shortcuts import get_object_or_404
# and from django.contrib.auth import ..., get_user_model
# Try retrieving the user object from the get_user_model method
user = get_object_or_404(get_user_model(), email=Email) # Assuming the email field is unique.
found = False
if user:
# If the user exists, then check his/her password
if user.check_password(Senha): # If the passwords matched
found = True
else:
print("Incorrect password!")
else:
print("User does not exist!")
if found: # If the password matched then log in the user
user = authenticate(username=user.username, password=password)
if user: # or if user is not None
login(request, user)
return redirect('home')
else:
print('The email or password is incorrect')
That's an approach you could take.