ValueError at /users/register/ The view users.views.register didn't return an HttpResponse object. It returned None instead

I am getting this error ValueError at /users/register/ The view users.views.register didn't return an HttpResponse object. It returned None instead.

I am using Django to complete the registration form, however I was unable to eliminate this problem.

def register(request):
      if request.method == 'POST':
            user_form = UserRegistrationForm(request.POST)
            if user_form.is_valid():
                  new_user = user_form.save(commit=False)
                  new_user.set_password(user_form.cleaned_data['password'])
                  new_user.save()
                  return render(request,'users/register_done.html')
            else:
                  user_form = UserRegistrationForm()
            return render(request,'users/register.html',{'user_form':user_form})

This is my views.py file made a register function with the return render.

path('register/',views.register,name='register'),

this is urls py

Is there any other solution to come over My result Output

I am expecting the solution for my problem

You also need to display error messages like if user already exits or password is empty or too-small.

Now the return statement is outside of if statement.

def register(request):

    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        if user_form.is_valid():
                new_user = user_form.save(commit=False)
                new_user.set_password(user_form.cleaned_data['password'])
                new_user.save()
                return render(request,'users/register_done.html')
        else:
                user_form = UserRegistrationForm()
    
    return render(request,'users/register.html',{'user_form':user_form})
Back to Top