0

I'm using Django's authenticate function to let users register (create an account) and sign in (login after creating an account). Authenticate works fine for registration, but when I try signing the user in after logging out, it doesn't work.

Registration Method:

if request.method == 'POST':
     form = SignUpForm(request.POST)
          if form.is_valid():
               form.save()
               username = form.cleaned_data['username']
               raw_password = form.cleaned_data['password1']
               user = authenticate(username=username, password=raw_password) #returns user object
               login(request, user) #works

Sign in Method:

if request.method == 'POST':
     form = AuthenticationForm(request=request, data=request.POST)
          if form.is_valid():
               username = form.cleaned_data['username']
               password = form.cleaned_data['password']
               user = authenticate(user=username, password=password) #returns None              
               login(request, user) #doesn't work

I looked at a few other threads that reported a similar issue and added the following code to my settings.py file. However, authenticate still returns none when I try signing in.

settings.py code

AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend',
    )

1 Answer 1

2

As per Django documentation for authenticate method, the valid keyword arguments are username and password. Thus, I would recommend changing the following in "Sign In" method:

user = authenticate(user=username, password=password)

to

user = authenticate(request=request, username=username, password=password)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.