3

Okay so I have a form and then in the template, I have this code:

{% if form.errors %}
    { form.errors %}
{% endif %}

Now, suppose I have a username field in my form which is required and I purposely do not fill it out. It would say

username
    -This field is required

How do I change the word "username" which it displays when telling what field the error message is for? I'm guessing it is taking the name from the Model I created because in the model (which I then made a form of) I had written

username = models.CharField(max_length=10)
first_name = models.CharField(max_length=20)

As you can see, if there is an error with the first_name field, it django would display

first_name
    - *error message*

How would I make it display "First Name" instead of "first_name"?

SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • did you considered to use verbose_name attribute on your model? you make like first_name = models.CharField(max_length=20, verbose_name="First Name") – robertof Sep 25 '13 at 02:30
  • 1
    Why are you using quotes in max_length values? – Fernando Freitas Alves Sep 25 '13 at 02:41
  • I tried using verbose_name but it didn't work for some reason.. is it supposed to work? I thought that the template ignores the verbose_name and that that is why it wasn't working.. is it supposed to work? Because if it is, then maybe there was something wrong with another part of my code which was causing the verbose_name to not work. – SilentDev Sep 25 '13 at 02:47
  • @FernandoFreitasAlves whoops sorry, max_length was not supposed to be in quotes. – SilentDev Sep 25 '13 at 02:48

1 Answers1

4

If you want to put some custom messages to your erros you may want to modify the clean method of your formfields.

For example:

class Form(forms.ModelForm):
    class Meta:
        model = User #Assuming User is the name of your model
        fields = ['username','first_name']

    def clean_first_name(self):
        if condition: # A condition that must raise an error
            raise forms.ValidationError(u'Your Custom Validation Error goes here.')
        return self.cleaned_data['first_name']

But if you just want to change the name that appears in a default error message you can put a verbose name in your fields, just like:

username = models.CharField(max_length=10, verbose_name="Username")
first_name = models.CharField(max_length=20, verbose_name="First Name")

or implicit:

username = models.CharField("Username", max_length=10)
first_name = models.CharField("First Name",max_length=20)

To see more about django form and field validation: https://docs.djangoproject.com/en/1.5/ref/forms/validation/#form-and-field-validation

About Override default Form error messages: Django override default form error messages

and

http://davedash.com/2008/11/28/custom-error-messages-for-django-forms/

Community
  • 1
  • 1
Fernando Freitas Alves
  • 3,709
  • 3
  • 26
  • 45
  • Hm okay.. is there a way / place where I can view all the default error messages which django has for a form who's field is a CharField? For example, I know that there will be an error message if the field is not filled in since it is required.. but is there a way to see when else django will return an error message? That way, I can customize all the error messages possible... Also, where did you learn about the "if condition: raise forms.ValidationError..." etc. from? Is there a link where I can read about how to use it? – SilentDev Sep 25 '13 at 02:46
  • I appended the references in the answer now. – Fernando Freitas Alves Sep 25 '13 at 02:57
  • Hi @FernandoFreitasAlves I am having trouble with the verbose name. This seems not working. Kindly see this snipper. http://pastebin.com/w6UkjzHF Thanks! – aldesabido Jul 16 '16 at 02:59