You can override django admin templates in several ways.
For example, there is django-project
as shown below:
django-project
|-core
| └-settings.py
|-app1
| |-models.py
| └-admin.py
|-app2
└-templates
Then, BASE_DIR / 'templates'
is set to DIRS
in TEMPLATES
in settings.py
so that templates
folder is recognized as shown below:
# "core/settings.py"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates', # Here
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
And, there are Food
and Drink
models in app1/models.py
as shown below:
# "app1/models.py"
class Food(models.Model):
name = models.CharField(max_length=20)
class Drink(models.Model):
name = models.CharField(max_length=20)
And, there are Food
and Drink
admins in app1/admin.py
as shown below:
# "app1/admin.py"
@admin.register(Food)
class FoodAdmin(admin.ModelAdmin):
pass
@admin.register(Drink)
class DrinkAdmin(admin.ModelAdmin):
pass
Now, you can override one of the django admin templates change_form.html in templates/admin/
, templates/admin/app1/
and templates/admin/app1/food/
as shown below. *You can copy django admin templates from django/contrib/admin/templates/admin/
in your virtual environment and some django admin templates cannot be overridden in templates/admin/app1/
or templates/admin/app1/food/
but these django admin templates can be overridden all in templates/admin/
, templates/admin/app1/
and templates/admin/app1/food/
and you can see my answer explaining which django admin templates can be overridden in which directories.
change_form.html
in templates/admin/
below can automatically apply to all admins in all apps. *The lowercase folder name admin
works properly:
django-project
|-core
| └-settings.py
|-app1
| |-models.py
| └-admin.py
|-app2
└-templates
└-admin
└-change_form.html # Here
change_form.html
in templates/admin/app1/
below can automatically apply to all admins in app1
. *The lowercase folder name app1
works properly:
django-project
|-core
| └-settings.py
|-app1
| |-models.py
| └-admin.py
|-app2
└-templates
└-admin
|-app1
| └-change_form.html # Here
└-app2
change_form.html
in templates/admin/app1/food/
below can automatically apply to food
admin in app1
. *The lowercase folder name food
works properly:
django-project
|-core
| └-settings.py
|-app1
| |-models.py
| └-admin.py
|-app2
└-templates
└-admin
|-app1
| |-food
| | └-change_form.html # Here
| └-drink
└-app2
And now, you can rename change_form.html
to custom_change_form.html
but custom_change_form.html
in any folders cannot automatically apply to any admins in any apps. So, you need to manually apply custom_change_form.html
to any admins in any apps which you want to apply custom_change_form.html
to.
For custom_change_form.html
in templates/admin/
below:
django-project
|-core
| └-settings.py
|-app1
| |-models.py
| └-admin.py
|-app2
└-templates
└-admin
└-custom_change_form.html # Here
Set admin/custom_change_form.html
to change_form_template in Food
and Drink
admins as shown below. *You can find more custom template options:
# "app1/admin.py"
@admin.register(Food)
class FoodAdmin(admin.ModelAdmin):
change_form_template = 'admin/custom_change_form.html'
@admin.register(Drink)
class DrinkAdmin(admin.ModelAdmin):
change_form_template = 'admin/custom_change_form.html'
For custom_change_form.html
in templates/admin/app1/
below:
django-project
|-core
| └-settings.py
|-app1
| |-models.py
| └-admin.py
|-app2
└-templates
└-admin
|-app1
| └-custom_change_form.html # Here
└-app2
Set admin/app1/custom_change_form.html
to change_form_template
in Food
and Drink
admins as shown below:
# "app1/admin.py"
@admin.register(Food)
class FoodAdmin(admin.ModelAdmin):
change_form_template = 'admin/app1/custom_change_form.html'
@admin.register(Drink)
class DrinkAdmin(admin.ModelAdmin):
change_form_template = 'admin/app1/custom_change_form.html'
For custom_change_form.html
in templates/admin/app1/food
below:
django-project
|-core
| └-settings.py
|-app1
| |-models.py
| └-admin.py
|-app2
└-templates
└-admin
|-app1
| |-food
| | └-custom_change_form.html # Here
| └-drink
└-app2
Set admin/app1/food/custom_change_form.html
to change_form_template
in Food
and Drink
admins as shown below:
# "app1/admin.py"
@admin.register(Food)
class FoodAdmin(admin.ModelAdmin):
change_form_template = 'admin/app1/food/custom_change_form.html'
@admin.register(Drink)
class DrinkAdmin(admin.ModelAdmin):
change_form_template = 'admin/app1/food/custom_change_form.html'