'Validation for UserCreationForm not working Django
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})
Solution 1:[1]
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
Solution 2:[2]
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})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hari Prasad |
| Solution 2 | Rajeel Rajput |
