0

Here is my models.py, im using these details for registration after registration i want the user to login with phone_number as his username and password. I tried custom ModelBackend but couldn't come with up solution

models.py

class userModel(models.Model):

Full_Name = models.CharField(max_length = 50)
Date_of_Birth = models.DateField()
Phone_Number = models.CharField(max_length = 10,unique=True,null=False,blank=False)
Email_ID = models.EmailField()
Passport_Number = models.CharField(max_length = 10)
photo = models.ImageField(upload_to='images/',max_length=255,blank=True,null=True)

def __str__(self):
    return self.Full_Name

views.py

def register(request):
   registered = False

   if request.method == 'POST':
       user_form = userForm(request.POST, request.FILES)

    if user_form.is_valid():
        user = user_form.save()
        user.save()
         registered = True
    else:
        print(user_form.errors)

else:
    user_form = userForm()

return render(request,'dezzexapp/registration.html',{'user_form':user_form,'registered':registered})
nischith
  • 1
  • 2

1 Answers1

0

About the part with using your phone number as login, you can refer this thread on logging in with another method (if you want both methods to stay, keep the old Django middleware; if you want to replace it, remove the existing auth middleware).

About the password part, I don't think that's necessary. As long as your phone numbers are unique, I believe that you can directly check whether the phone number matches a user in the database. However, if you insist on two passwords, here's how you should do it.

For the example that I provided you with, you might need two password fields for a user, one for the regular one and another for the phone. But modify like this:

if user.check_password(password1) or user.check_password(password2):

and then that should make both passwords valid for the user.

crimsonpython24
  • 2,223
  • 2
  • 11
  • 27