2

Is it possible to change django admin error message.

i want add my custom error message.

Please correct the errors below. change this message

class mymodel(admin.ModelAdmin):

    # change message

enter image description here

rahul.m
  • 5,572
  • 3
  • 23
  • 50

2 Answers2

2

Carefully reading the documentation, we can see that we can override the base translations, as follows:

  1. In your settings.py, add/update the following variable:

    LOCALE_PATHS = [
        os.path.join(BASE_DIR,"locale"),
    ]
    
  2. In your project's base directory, (where you find manage.py), create a folder named locale (or whatever, but if you rename it differently, change in step 1 as well).
  3. In locale folder, create a folder for each language that you want to override. In our case, we want to override english, so we need to create a folder named en
  4. In the en folder, create yet another folder, named LC_MESSAGES
  5. In the LC_MESSAGES folder, create an empty file named django.po

At this point, this is what the contents of locale should look like

├── locale
│   └── en
│       └── LC_MESSAGES
│           └── django.po
  1. Now we need to add in this django.po file whatever string we want to override from Django base translation. You can find them in the source code. For example, in your specific case, this file tells us that the string id we need to override is on line 459:

    msgid "Please correct the errors below."
    
  2. We use that id to provide a different string, so add the following to the django.po file:

    msgid "Please correct the errors below."
    msgstr "Fix the errors now!"
    

In this case, I took the original message and replaced it with "Fix the errors now!"

  1. Recompile the messages with

    django-admin compilemessages
    

    This command should output should output a message like this:

    processing file django.po in /path/to/project/locale/en/LC_MESSAGES
    
  2. Django will now consider this file with higher priority and display the new message:

enter image description here

Adelin
  • 7,809
  • 5
  • 37
  • 65
2

One of the ways to solve this is by using translations, but that would require knowledge in editing .PO files and you will have to make a lot of changes to your code.

Fortunately, Django allows you to include extra .css files and .js files to a ModelAdmin.

The tricky idea is that you will create a javascript file in your static directory, and inside that file you will replace whatever text you wish

/static

    admin.js

admin.js

// When the page is fully loaded

document.addEventListener('DOMContentLoaded', function()
{
    // Replace whatever text you wish, line by line
    document.body.innerHTML = document.body.innerHTML.replace(new RegExp('\\bPlease correct the errors below\\b', 'g'), 'Some fields are empty');
    document.body.innerHTML = document.body.innerHTML.replace(new RegExp('\\bThis field is required\\b', 'g'), 'This field cannot be empty');
});

admin.py

class mymodem(admin.ModelAdmin):
    class Media:
        js = ['admin.js']

    ....

OUTPUT :

enter image description here

enter image description here

Amine Messaoudi
  • 2,141
  • 2
  • 20
  • 37