Title: phonenumber field authentication error in django
Tags: python,django,django-models,django-views,django-forms

Question: I am making a login function in django where user will input their phone number and password, they can access the account. but when I run the server, I get error that invalid phone number and password, but I registered proper phone number and password. I did some troubleshooting and come to know that phone number data is not showing. here's my code
models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from phonenumber_field.modelfields import PhoneNumberField
from .manager import MyUserManager

# Create your models here.


class MyUser(AbstractBaseUser, PermissionsMixin):
    username= models.CharField(max_length=50, null=True)
    phone_number = PhoneNumberField(unique=True)
    first_name = models.CharField(max_length=30, blank=True)
    last_name = models.CharField(max_length=30, blank=True)
    email = models.EmailField(unique=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    USERNAME_FIELD = 'phone_number'
    REQUIRED_FIELDS = []

    objects = MyUserManager()

    def __str__(self):
        return self.phone_number.as_e164
    
   

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

forms.py
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from phonenumber_field.formfields import PhoneNumberField
from .models import *

class MyLoginForm(AuthenticationForm):
    phone = PhoneNumberField()
    password = forms.CharField(label='Password', widget=forms.PasswordInput)


views.py
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import render, redirect
from .forms import MyLoginForm

# Create your views here.

def my_login(request):
    if request.method == 'POST':
        form = MyLoginForm(request, request.POST)
        if form.is_valid():
            phone_number = form.cleaned_data.get('phone')
            print(form.cleaned_data.get('phone_number'))
            password = form.cleaned_data.get('password')
            print(password)
            user = authenticate(request, phone_number=phone_number, password=password)
            if user is not None:
                login(request, user)
                return redirect('home')
            else:
                form.add_error(None, 'Invalid phone number or password.')
    else:
        form = MyLoginForm()
  
    return render(request, 'core/login.html',  {'form': form})

can anyone tell me where I am doing wrong?

Answer: You should access phone_number field not phone since you are authenticating with phone_number field in authenticate() so, you view code should be like this:

def my_login(request):
    if request.method == 'POST':
        form = MyLoginForm(request.POST)
        if form.is_valid():
            phone_number = form.cleaned_data.get('phone_number')
            print(form.cleaned_data.get('phone_number'))
            password = form.cleaned_data.get('password')
            print(password)
            user = authenticate(request, phone_number=phone_number, password=password)
            if user is not None:
                login(request, user)
                return redirect('home')
            else:
                form.add_error(None, 'Invalid phone number or password.')
    else:
        form = MyLoginForm()
  
    return render(request, 'core/login.html',  {'form': form})

The irony is that when you print it as print(form.cleaned_data.get('phone_number')) it is showing as you said in question :)