1

There were a couple of other similar questions with answer but none of them fixed my problem. I am getting 'You have multiple authentication backends configured and therefore must provide the backend argument or set the backend attribute on the user' every time I try to create a new user using by sign up in Django. Here is my view:

def signupUser(request):

    if request.method == 'POST':
        form = SignUpForm(request.POST)

        if form.is_valid():
            form.save(commit=False)
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            form.save()
            user.backend = 'django.contrib.auth.backends.ModelBackend'
            login(request, user)
            return redirect('home')
    else:
        form = SignUpForm()

    return render(request, 'signup.html', {'form': form})

I had added user.backend = 'django.contrib.auth.backends.ModelBackend' in hopes of fixing the problem, but it did nothing.

My forms.py:

class SignUpForm(UserCreationForm):
    email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
    password1=forms.CharField(help_text='Your password cant be too similar to your other personal information. Your password must contain at least 8 characters. Your password cant be a commonly used password. Your password cant be entirely numeric.', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

By the way, in my AUTHENTICATION_BACKENDS are 'django.contrib.auth.backends.ModelBackend' and 'allauth.account.auth_backends.AuthenticationBackend',

2 Answers2

1

There are a few errors in your view function. I don't know how this worked out for you, but for the sake of clarity, I'd like to point out some of it.

Primarily, I am re-writing your view to a more suitable way,

def signupUser(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)

        if form.is_valid():
            form.save(commit=False)
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            form.save()
            user = authenticate(username=username, password=raw_password)
            if user is not None:
                login(request, user)
                return redirect('home')
    else:
        form = SignUpForm()

    return render(request, 'signup.html', {'form': form})

Here, the view is meant to be for creating new users. So, you may need to call form.save() method before calling authenticate() function. Before committing to the database you are trying to authenticate the user which is not created yet. Check if your view does actually logs in any user. Then, authenticate() function returns None, if there is any mismatch in the credentials, so, check that too. Or else, Django would raise an AttributeError, when calling login() function.

I seriously doubt that your view logs in any user, because you are redirecting to same page regardless of the success of logging in. Its quite counter-intuitive, makes it harder to debug. Simply redirect to the home page when login is successful and to the login page if its not.

zaidfazil
  • 9,017
  • 2
  • 24
  • 47
0

I realized that a user was still being created even though this error was showing up, so I guess it gets the job done. But to get rid of this error, I changed my view to this:

def signupUser(request):

    if request.method == 'POST':
        form = SignUpForm(request.POST)

        if form.is_valid():
            form.save(commit=False)
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            form.save()

            try:
                login(request, user)
            except ValueError:
                return redirect('home')

            return redirect('home')
    else:
        form = SignUpForm()

    return render(request, 'signup.html', {'form': form})

This strangely(at least strangely to me) worked out. A user was created and it can be properly logged into. I don't think this has any bad side effects, but I will wait to mark my solution correct just in case that there is a better solution.