[Python] Django: Unable to login into a account

Discussão em 'Python' iniciado por Stack, Janeiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have created a custom user model in my django model where the password is being saved using make_password from password_strength but when trying to login using the check_password it says invalid username or password.

    @csrf_exempt
    def login_attempt(request):
    if request.method == 'POST':
    try:
    data = json.loads(request.body)
    email = data.get('email')
    password = data.get('password')

    try:
    user_obj = user.objects.get(email=email)
    except user.DoesNotExist:
    return JsonResponse({'success': False, 'message': "Email doesnot exists"}, status=401)

    if check_password(password, user_obj.password):
    login(request, user_obj)
    return JsonResponse({'success': True, 'message': "Login successful"}, status=200)
    else:
    return JsonResponse({'success': False, 'message': "Invalid email or password"}, status=401)

    except Exception as e:
    return JsonResponse({'success': False, 'message': f"Error: {str(e)}"}, status=500)

    return JsonResponse({'success': False, 'message': "Invalid request method"}, status=405)

    Continue reading...

Compartilhe esta Página