The view account.views.register did not return an HttpResponse object. It returned None instead.

325 Views

The view account.views.register did not return an HttpResponse object. It returned None instead.

Django | The view account.views.register didn't return an HttpResponse object. It returned None instead.

This error usually occurs when missed writing a return before render. The render is used to render the view to the browser. It can not be just called, it must be returned.

consider below code as an example:

 def register(request):

    if request.method == 'POST':
        username = request.POST['username']
        email = request.POST['email']
        password = request.POST['password']
    
        user = User.objects.create_user(username=username, email=email, password=password)
        user.save()
		
        print('User Created')
		
        redirect('/')
        
    else:
        return render(request, 'register.html') 

The above code generates an error message of "The view account.views.register didn't return an HttpResponse object. It returned None instead." because the return is not written in front of "redirect('/')".

ValueError at /account/register
The view account.views.register didn't return an HttpResponse object. It returned None instead.
Request Method:	POST
Request URL:	http://127.0.0.1:8000/account/register
Django Version:	3.0.2
Exception Type:	ValueError
Exception Value:	
The view account.views.register didn't return an HttpResponse object. It returned None instead.

This error generated because the above code return is missing before render. The view must be "return render". it can not be just called.

So to remove the error, just put a return in front of "redirect('/')"

 def register(request):

    if request.method == 'POST':
        username = request.POST['username']
        email = request.POST['email']
        password = request.POST['password']
    
        user = User.objects.create_user(username=username, email=email, password=password)
        user.save()
		
        print('User Created')
		
        return redirect('/')
        
    else:
        return render(request, 'register.html') 

The above code does not show any error it saves the data to the database it redirects to the base URL.

 


 

Let's check out the code with some basic validation:

 def register(request):

    if request.method == 'POST':
        username = request.POST['username']
		email = request.POST['email']
        password = request.POST['password']
        confirm_password = request.POST['confirm_password']
        

        if password==confirm_password:
            if User.objects.filter(username=username).exists():
                messages.info(request, 'Username Taken')
                return redirect('register')
            elif User.objects.filter(email=email).exists():
                messages.info(request, 'Email Taken')
                return redirect('register')
            else:
                user = User.objects.create_user(username=username, email=email, password=password)
                user.save()
				
                return redirect('/')
                
        else:
            messages.info(request, 'Password does not match')
            return redirect('register')
    else:
        return render(request, 'register.html')

 

Tags:-
Python
Django