Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to submit this User model, but when I try to create a user, it throws me an error: authenticate() takes from 0 to 1 positional arguments, but 2 were given . While it throws this error, it successfully creates the user with no password when I check my admin

How can I solve this problem?

Models.py:

class User(AbstractUser):     username = models.CharField(max_length=70, unique=True)     email = models.EmailField(unique=True)     phone_number = models.CharField(max_length=15)     USERNAME_FIELD = 'email'     REQUIRED_FIELDS = ['username', 'first_name', 'last_name', 'phone_number', 'password']     def __str__(self):         return "{}".format(self.email)

Views.py:

def create_user(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = form.save()
            authenticate(request, user)
            return redirect('Home')
    else:
        form = RegistrationForm()
    return render(request, 'register.html, {'form':form})

Forms.py:

class RegistrationForms(forms.Form): 
     username = forms.CharField(label="Username", max_length=70, 
     widget=forms.TextInput(attrs={'class':'form-control'})) 
     first_name = forms.CharField(label="First Name", 
     widget=forms.TextInput(attrs={'class':'form-control'})) 
     last_name = forms.CharField(label="last Name", 
     widget=forms.TextInput(attrs={'class':'form-control'})) 
     email = forms.EmailField(label="School Email", 
     widget=forms.EmailInput(attrs={'class':'form-control'})) 
     phone_number = PhoneNumberField(label="School Phone Number", 
     widget=forms.NumberInput(attrs={'class':'form-control'})) 
     password = forms.CharField(label="Password", 
     widget=forms.PasswordInput(attrs={'class':'form-control'})) 
     repeat_password = forms.CharField(label="Repeat Password", 
     widget=forms.PasswordInput(attrs={'class':'form-control'})) 
                What are you trying to do here? At first form.save() is called on a ModelForm not on a form made through normal Form class.
– Sunderam Dubey
                Mar 15 at 10:00
                @SunderamDubey After changing my form class from: Form to ModelForm the error isn't gone. It still shows this error.
– user21388606
                Mar 15 at 10:05
                Do you also want to authenticate and login the user in the create_user view or simply want to save the form and redirect the user to some page?
– Sunderam Dubey
                Mar 15 at 10:06

I think you need pass user credentials like this...

def create_user(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = form.save()
            authenticate(request, username=request.POST.get('username'),password=request.POST.get('password'))
            # ------------- OR ----------------
            authenticate(request, username=form.cleaned_data['username'],password=form.cleaned_data['password'])
            return redirect('Home')
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.