Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Authenticate always returns None with correct credentials also

I was creating a login and registration page for my project and was using the Django MySQL database for login and creating users the signup was done correctly but the login function is not working for me.

PS: The is_active is Set True but still this is not working

With all correct info, it shows None. I have given all the necessary parts of the file and codes

I tried everything but nothing seems to work also tried some of the solutions listed on StackOverflow but still, nothing worked for me.

from django.shortcuts import render, redirect
from .models import Description
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login

# Create your views here.
def index(request):
    d = Description.objects.all()
    return render(request,'index.html',{'des':d})
def signup(request):
    if request.method == 'POST':
        first_name = request.POST['first']
        last_name = request.POST['last']
        user_name = request.POST['user']
        email = request.POST['email']
        pass1 = request.POST['password1']
        pass2 = request.POST['password2']
        if pass1 == pass2:
            if User.objects.filter(email=email).exists():
                print("Email taken")
            elif User.objects.filter(username=user_name).exists():
                print("Username taken")
            else:        
                user = User.objects.create_user(username = user_name, first_name=first_name,last_name=last_name, password=pass1, email=email)
                user.save()
                print("User created")
        else:
            print("Password Not Matching")  
        return redirect('/')      
    else:
        return render(request,'signup.html')   
def login(request):
    if request.method == 'POST':
        email = request.POST['email']         
        password = request.POST['password']
        print(email,password)
        user = authenticate(request, email=email, password=password)
        print(user)
        if user is not None:
            login(request, user)
            print("Logged in")
        else:
            print("not logged in")    
        return redirect('/')
    else:
        return HttpResponse("Invalid")        
like image 710
Subham Banerjee Avatar asked Jan 17 '26 08:01

Subham Banerjee


1 Answers

Django authenticate method allows for authenticating using only username. You can get the user's username from the User model using the user's email

username = User.objects.get(email=email).username
password = request.POST.get('password')

Replace this user = authenticate(request, email=email, password=password)

with this user = authenticate(request, username=username, password=password)

like image 200
Romeo Avatar answered Jan 19 '26 21:01

Romeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!