[Answered ]-Value error. The view 'users.views.register' didn't return an HttpResponse object. It returned None instead

1👍

your views.py should be in this way

from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import UserCreationForm


def register(request):
    # return HttpResponseRedirect(reverse('index'))
    print(request.method)
    import pdb; pdb.set_trace()
    if request.method != "POST":
        form = UserCreationForm()
        return render(request, 'users/register.html', {'form':form})
    else:
        print('\nelse\n')
        form = UserCreationForm(request.POST)
        print('\n'+str(form.is_valid())+'\n')
        if form.is_valid():
            new_user = form.save()
            authenticated_user = authenticate(username=new_user.username, 
                                              password=request.POST['password1'],
                                              confirm_password=request.POST['password2'])
            login(request, authenticated_user)
            return HttpResponseRedirect(reverse('index'))
            context = {'form': form}
            return render(request, 'users/register.html', context)

The error is because you haven’t returned anything in the view when the request.METHOD is not POST

Leave a comment