Using Default UserCreationForm from django.contrib.auth.forms does not show error if the passwords do not match. I do not want to create a custom model.
Here is the code from my views.py file
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render
# Create your views here.
def register(response):
if response.method == "POST":
print(response.POST)
form = UserCreationForm(response.POST)
if form.is_valid():
form.save()
else:
print(form.errors)
form = UserCreationForm()
return render(response,"register/register.html",{"form":form})
register.html code
<html>
<head>
<title>Register </title>
</head>
<body>
<h1>This is the Registration page</h1>
<form method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Submit</button>
</form>
</body>
</html>
I thought the message would be automatically displayed. Am I missing something? Edit: None of the error message are being displayed like password length < 8, all numeric passwords, etc.
CodePudding user response:
You each time create a new form, you should render the one that is invalid:
from django.shortcuts import redirect
def register(response):
if response.method == 'POST':
form = UserCreationForm(response.POST)
if form.is_valid():
form.save()
return redirect('name-of-some-view')
else:
form = UserCreationForm() #