Home > Enterprise >  Validation for UserCreationForm not working Django
Validation for UserCreationForm not working Django

Time:04-30

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})


CodePudding user response:

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 version 1.7, Django counts,before returning the response object.

CodePudding user response:

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})
  • Related