How to change error_messages for model fields?

Hello good Django people!

I am reading the docs but it is a bit confusing. I don’t understand where and which error messages are being applied.


I have a Course model with a title field.

class Course(models.Model):
    class Level(models.TextChoices):
        BEGINNER = 'BEGINNER', 'Beginner'
        INTERMEDIATE = 'INTERMEDIATE', 'Intermediate'
        ADVANCED = 'ADVANCED', 'Advanced'
    title = models.CharField(max_length=150, unique=True,
                    error_messages ={
                    "unique":"Kurs sa ovim imenom vec postoji."
    slug = models.SlugField(max_length=160, unique=True,
                    error_messages ={
                    'unique':'Kurs sa ovom url adresom vec postoji.',
                    'slug_invalid':'Unesite ipravnu url adresu, dozvoljeni su brojevi, slova, donje crte i minus crte.',

This is the template

      {{ form.title.label_tag }}
      {{ form.title }}
      {% for error in form.title.errors %}
        <p class="error">{{ error }}</p>
      {% endfor %}
      {{ form.slug.label_tag }}
      {{ form.slug }}
      {% for error in form.slug.errors %}
        <p class="error">{{ error }}</p>
      {% endfor %}

But for the slug field, if entered characters are not okay, i still get the default:
“Enter a valid “slug” consisting of letters, numbers, underscores or hyphens.”
I also tried the key ‘invalid’ but it is not showing up.

Can somebody please explain where and how i can change the text for error messages for model fields? Basically all i want is to translate them into Serbian.

Thank you

One issue here is that there’s the possiblity that the data is being validated in four different places.

  • The browser
  • JavaScript
  • Django Form
  • Django model

The first step is to figure out what is generating the message.

If you’re getting the error without the data being posted to the server, then the validation is being done in the browser. (Either by the browser itself or JavaScript running in the browser.) If the data is posted, then it’s being generated either by the form or the model.

Hi, thank you.

The data is being posted, i mean i fill in the form and click to submit. Then i see all corresponding errors. But this invalid error is not being shown, only unique error.

Is there a general rule of how you would go, if you just want the change the text in those errors on submit?

I tried this:

        error_messages = {
            'title':{'unique': 'naslov vec postoji'},
                'unique': 'slug vec postoji',
                'invalid':'slug nije ispravan'},

This is in class Meta in for that Course model.

Looks like it is working.

Glad to see you have it working. But I’d like to add a side note for your previous post.

This does not necessarily mean that the data was posted to the server. You need to look at the requests being made to the server (either the runserver console or your browser’s developer tools network tab) to see if the POST was being made.

Since you’ve fixed this in the form, the data is being posted. But this isn’t necessarily always going to be the case.

Thank you.

I got it to work only for a few fields. But many of them are not working like order field for Lesson that has unique together with Course. Besides that, some errors get shown under the field and some can be shown only with:

      {% if form.non_field_errors %}
      <span style='color:red;'>
          {{ form.non_field_errors }}
      {% endif %}

About what you said for POST requests, i didnt really understand, but thank you.