[Answered ]-MultiValueDictKeyError: "'password'"

2👍

If you check UserCreationForm, you’ll see that it doesn’t have a field named password, but it has two: password1 and password2. You can change your code to this:

if form.is_valid():
    new_user = form.save()
    # we use the date to redirect our user to the page of login
    authenticated_user = authenticate(username=new_user.username,
                                      password=form.cleaned_data['password1'])
    #                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    login(request, authenticated_user)
    return HttpResponseRedirect(reverse('learning_logs:index'))

However, you probably want to avoid dealing with passwords and simply use login() directly, without passing through authenticate():

if form.is_valid():
    user = form.save()
    login(request, user)
    return HttpResponseRedirect(reverse('learning_logs:index'))

Leave a comment