The Python Oracle

In Django, how do i change "This field is required." to "Name is required"?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3

--

Chapters
00:00 In Django, How Do I Change &Quot;This Field Is Required.&Quot; To &Quot;Name Is Required&Quot;?
00:23 Answer 1 Score 0
00:42 Accepted Answer Score 23
01:03 Thank you

--

Full question
https://stackoverflow.com/questions/4466...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #forms #frameworks

#avk47



ACCEPTED ANSWER

Score 23


An easy way to specify simple "required" validation messages is to pass the field the error_messages argument.

name = forms.CharField(error_messages={'required': 'Your Name is Required'}) 

Check the docs for which keys can be specified per field: http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.Field.error_messages

For anything else, you're going to need real form validation which means you'd be writing error messages anyways!




ANSWER 2

Score 0


If you want to customize the message a little bit more you can also:

from django.core.exceptions import ValidationError

def my_validator(value):
    if not len(value):
        raise ValidationError('Your error message here!')

Then, in your models.py:

from django import forms

class MyForm(forms.Form):
    my_field= forms.CharField(validators=[my_validator])