0

Everything seems to be working fine, but if the form doesn't validate, instead of getting HTML validation errors, I get ValueError ar /register/: The user.register didn't return an HTTPResponse. It returned none instead.

My code:

if request.method == 'POST':
   form = UserCreationForm(request.POST)
   if form.is_valid():
      form.save()
      messages.success(request, 'Acount created!')
else:
   form = UserCreationForm():
   return render(request, 'users/register.html', {"form":form})


  • Does this answer your question? [Why Python recursive function returns None](https://stackoverflow.com/questions/31221826/why-python-recursive-function-returns-none) – BrokenBenchmark Apr 30 '22 at 05:48
  • I realize that your function isn't recursive, but the root cause is the same; you're not returning anything in the `if` statement. – BrokenBenchmark Apr 30 '22 at 05:49

2 Answers2

0

usually response are returned on each api call

like

import json
from django.http import HttpResponse

def profile(request):
    data = {
        'name': 'Vitor',
        'location': 'Finland',
        'is_active': True,
        'count': 28
    }
    dump = json.dumps(data)
    return HttpResponse(dump, content_type='application/json')

try to add return statement with any of Response from django based on what you try to return

you need to add similar to this on if block

refer : https://simpleisbetterthancomplex.com/tutorial/2016/07/27/how-to-return-json-encoded-response.html#:~:text=Since%20version%201.7%2C%20Django%20counts,before%20returning%20the%20response%20object.

Hari Prasad
  • 461
  • 1
  • 3
  • 9
0
form = UserCreationForm()
if request.method == 'POST':
   form = UserCreationForm(request.POST)
   if form.is_valid():
      form.save()
      messages.success(request, 'Acount created!')
      return # render any template or redirect to any view you want after account creation
return render(request, 'users/register.html', {"form":form})