Why can't I override the Django Admin template?
Asked Answered
P

2

4

I can't change my Django admin template. I have followed the instructions from the Django documentation, and I have already checked StackOverflow questions.

I have already made /templates/admin/my_app/, and changed base_site.html in /templates/admin/my_app/base_site.html

settings.py:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

base_site.html:

#base_site.html
{% extends "admin/base_site.html" %}

{% block title %}{{ title }} | {{ site_title|default:_('NEW TITLE') }}{% 
endblock %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ 
site_header|default:_('NEW TITLE') }}</a></h1>
{% endblock %}

{% block nav-global %}{% endblock %}

File Structure:

my_project
└── my_project
    └── templates
        └── admin
           └── my_app
                └── base_site.html

Why can't I override the Django Admin template?

Pleasance answered 8/2, 2019 at 10:54 Comment(2)
Try to refresh page by Ctrl+F5Lucaslucca
still not workingPleasance
A
2

base_site.html should be placed in /templates/admin folder itself as shown below:

/templates/admin/base_site.html instead of /templates/admin/my_app/base_site.html

Acetaldehyde answered 8/2, 2019 at 10:58 Comment(1)
I thought I had tried them all, this finally solved it. I am using Django 2.2, for the records, and no other SO answer seemed to solve it, nor the 2.2 Django Docs which suggested exactly the OP structure.Discernment
D
1

base_site.html can be automatically overridden only in /templates/admin/ but not in /templates/admin/<app>/ and /templates/admin/<app>/<model>/.

According to my experiments and the doc, the django admin templates in django/contrib/admin/templates/admin/ in your virtual environment below can be automatically overridden all in /templates/admin/, /templates/admin/<app>/ and /templates/admin/<app>/<model>/. *app_index.html is not meaningful to override in /templates/admin/<app>/<model>/:

actions.html  
app_index.html # *Not meaningful to override in `/templates/admin/<app>/<model>/`  
change_form.html  
change_form_object_tools.html  
change_list.html  
change_list_object_tools.html  
change_list_results.html  
date_hierarchy.html  
delete_confirmation.html
delete_selected_confirmation.html
object_history.html  
pagination.html  
popup_response.html  
prepopulated_fields_js.html  
search_form.html  
submit_line.html

And according to my experiments, the django admin templates in django/contrib/admin/templates/admin/ in your virtual environment below can be automatically overridden only in /templates/admin/:

app_list.html
base.html
base_site.html
filter.html
index.html
login.html
nav_sidebar.html

And according to my experiments, the django admin templates in django/contrib/admin/templates/admin/auth/user/ in your virtual environment below can be automatically overridden only in /templates/admin/auth/user/:

add_form.html
change_password.html

And according to my experiments, the django admin templates in django/contrib/admin/templates/admin/edit_inline/ in your virtual environment below can be automatically overridden only in /templates/admin/edit_inline/:

stacked.html
tabular.html

And according to my experiments, the django admin templates in django/contrib/admin/templates/admin/includes/ in your virtual environment below can be automatically overridden only in /templates/admin/includes/:

fieldset.html
object_delete_summary.html

And according to my experiments, the django admin templates in django/contrib/admin/templates/admin/widgets/ in your virtual environment below can be manually overridden under /templates/*:

clearable_file_input.html
foreign_key_raw_id.html
many_to_many_raw_id.html
radio.html
related_widget_wrapper.html
split_datetime.html
url.html
Dresden answered 12/5, 2023 at 8:35 Comment(1)
I don't know why, just works in exactly that way your describe. I'm using Django 4.2Mahican

© 2022 - 2024 — McMap. All rights reserved.