Django form.errors not showing up in template

Multi tool use
Django form.errors not showing up in template
I have refrenced this stackoverflow page and tried to display my forms error on the html template.
I did:
{% if form.error %}
{% for field in form %}
{% for error in field.errors %}
{{ error|escape }}
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
{{ error|escape }}
{% endfor %}
{% endif %}
as said in the stackoverflow question I also tried simply doing:
{% if form.error %}
This should pop up if there is an error
{% endif %}
Nothing comes up regardless:
Heres my view.py code:
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password2')
user = authenticate(username=username, password=password)
login(request, user)
return HttpResponseRedirect('/')
else:
print(form.errors)
form = UserCreationForm()
return render(request, 'registration/register.html', {'form': form})
I am able to get the form errors onto the django console but it refuses to show up on the template.
Printing form.errors
prints to the console: <li>password2<ul class="errorlist"><li>The two password fields didn't match.</li></ul></li></ul>
form.errors
<li>password2<ul class="errorlist"><li>The two password fields didn't match.</li></ul></li></ul>
1 Answer
1
forms.errors
fired up, but at the end, you declare a new form form = UserCreationForm()
just before you render your view.
forms.errors
form = UserCreationForm()
After checking whether the form
is valid or not, all your validation errors are inside form
instance, remember processes run as sequence, at the end, you destroy the variable form
with form = UserCreationForm()
so no validation errors anymore.
form
form
form
form = UserCreationForm()
What you can do is add this new form form = UserCreationForm()
to else statement when your request method is GET
to keep having an empty form. By adding the else
statement you avoid the new assignment of the form; after the validation process, it will jump to render(request,....)
with the form
instance containing all validation errors
form = UserCreationForm()
GET
else
render(request,....)
form
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password2')
user = authenticate(username=username, password=password)
login(request, user)
return HttpResponseRedirect('/')
else:
print(form.errors)
else:
form = UserCreationForm()
return render(request, 'registration/register.html', {'form': form})
Note, the correct call for form errors in templates is form.errors
with s not form.error
form.errors
form.error
{% if form.error %} {% if form.errors %}
conventionally yes, but there are plenty other ways. you create a form with
request.POST
when request is POST, and an empty form when it's GET– Lemayzeur
Jul 3 at 3:21
request.POST
I would have never guessed that issue thanks
– Divise
Jul 3 at 3:23
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Should I create 1 form for both post and get requests ?
– Divise
Jul 3 at 3:19