Валидация для UserCreationForm не работает Django

Все вроде бы работает нормально, но если форма не проходит валидацию, то вместо ошибки HTML валидации я получаю ValueError ar /register/: User.register не вернул HTTPResponse. Вместо этого он не вернул ничего.

Мой код:

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


обычно ответы возвращаются при каждом вызове api

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

попробуйте добавить оператор return с любым из Response из django в зависимости от того, что вы пытаетесь вернуть

вам нужно добавить подобное в блок if

refer : https://simpleisbetterthancomplex.com/tutorial/2016/07/27/how-to-return-json-encoded-response.html#:~:text=После%20версии%201.7%2C%20Django%20считает,перед%20возвращением%20ответа%20объекта.

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})
Вернуться на верх