Custom form error messages: how to replace the field name?


Currently, when a validation error message appears on my form, it looks like this:

* name_of_field
    * This field is mandatory.


Is there a way to customize these messages?

I’d like to make it more like this:

* "What's your name?"
    * This field is mandatory.

“What’s you name?” being a label_tag.


This actually depends upon whether you’re doing client-side or server-side validation for these fields.

If you’ve got client-side validation happening, see Client-side form validation - Learn web development | MDN

If you are performing server-side validation, then you can define these messages in your validators or using the error_messages attribute on the field definitions. (Also see Creating forms from models | Django documentation | Django when working with model forms.)

1 Like