[Answered ]-Django crispy forms error message

2👍

Use the messaging system

from django.contrib import messages

...
else:
    messages.error(request, "Your error message")
    loginForm = UserLoginForm()
....

Check on the django.contrib.messagges doc.

0👍

You may also want to check and see if you are getting the HTML5 required attribute added to the form element tags for the elements that you have defined as required in your models.py.

If you are, and required elements are missing from your form, the HTML5 client-side validation in your browser prevents the form submitting, so you don’t get a request.POST and so no Django server-side validation and thus no form.errors are returned.

I spent a good few days puzzling out this one. Anyways, starting with Django 1.10, you can turn off the HTML5 required attribute in the form elements* and rely on the server-side Django validation (and template error messages).

*In my testing the HTML5 form validation bubble support was hit/miss and I have users in locked down machines who may not have the latest, greatest browser version.

Leave a comment