Search code examples
pythondjangodjango-forms

what is Password-based authentication in the UserCreationForm in Django?


I creat a signup form in django using django forms and when i run my code there is field i didnt expect Password-based authentication i did not use it and i have no idea what it is so anyone can tell me what it is and how i can remove it from user signup form? signup form

form.py

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.hashers import check_password

class RegisterForm(UserCreationForm):
    """Form to Create new User"""
    def __init__(self, *args,hashed_code=None, **kwargs) -> None:
        super(RegisterForm,self).__init__(*args, **kwargs)
        self.hashed_code = hashed_code

    
    code = forms.CharField(max_length=4,
                           required=True,
                           label="code",
                           help_text="Enter the four-digit code"
                           )
    
    def is_valid(self):
        """Return True if the form has no errors, or False otherwise."""
        if not self.hashed_code:
            self.add_error("code","you have not any valid code get the code first")
        elif not check_password(self.data.get("code"),self.hashed_code) :
            self.add_error("code","code is invalid")
        return self.is_bound and not self.errors
        
    
    
    class Meta:
        model = get_user_model()
        fields = ["email", "password1", "password2","code"]

Solution

  • That field comes from BaseUserCreationForm, the superclass of UserCreationForm.

    This is actually a regression in Django 5.1, and will be fixed when https://github.com/django/django/pull/18484 is released in Django 5.1.1.

    As a workaround, you should be able to delete the field from your subclass with

    class RegisterForm(UserCreationForm):
        usable_password = None  # Workaround; see https://github.com/django/django/pull/18484