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
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
Carefully reading the documentation, we can see that we can override the base translations, as follows:
In your settings.py
, add/update the following variable:
LOCALE_PATHS = [
os.path.join(BASE_DIR,"locale"),
]
manage.py
), create a folder named locale
(or whatever, but if you rename it differently, change in step 1 as well).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
en
folder, create yet another folder, named LC_MESSAGES
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
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."
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!"
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
Django will now consider this file with higher priority and display the new message:
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
// 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');
});
class mymodem(admin.ModelAdmin):
class Media:
js = ['admin.js']
....
OUTPUT :