Dean
Dean

Reputation: 1236

How do I override Django's default admin templates and layouts

I am trying to override Django's default template. For now just the base_site.html. I'm trying to change the text django administration.

I did the following:

  1. I created a folder in my app directory /opt/mydjangoapp/templates/admin
  2. I then copied the original django admin templates folder contents into admin and here are the contents: 404.html auth change_list.html delete_selected_confirmation.html index.html pagination.html search_form.html 500.html base.html change_list_results.html edit_inline invalid_setup.html popup_response.html submit_line.html actions.html base_site.html date_hierarchy.html filter.html login.html prepopulated_fields_js.html app_index.html change_form.html delete_confirmation.html includes object_history.html related_widget_wrapper.html
  3. I changed the contents of base_site.html, so that I have the title 'My App Admin' as opposed to 'Django Administration'
  4. My settings file looks as follows:

    INSTALLED_APPS = (
        'mydjangoapp',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'debug_toolbar',
    )
    
    ...
    
    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': ['/opt/mydjangoapp/templates/'],
            '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',
                ],
                'debug':True,
                'loaders': (
                     'django.template.loaders.filesystem.Loader',
                     'django.template.loaders.app_directories.Loader'
                ),
            },
        },
    ]
    

But unfortunately, Django version 1.8 seems to be ignoring my template changes and loading the original template files. Any suggestions as to how I can override the original layout for the admin. Bare in mind changing the title is just the beginning of the changes that I want to perform?

Upvotes: 2

Views: 3167

Answers (1)

Paulo Scardine
Paulo Scardine

Reputation: 77251

First, make sure your application is listed first, because Django always take the resources from the first application where it finds them, according to the order in settings.INSTALLED_APPS.

In this case the app was listed first, but the override was not working because the templates were placed under the global template directories listed under settings.TEMPLATES['DIRS'] which are not tied to any particular app and will have the least priority.

If this is your case, you must move the template folder inside your app (for example, /opt/mydjangoapp/mydjangoapp/templates/ instead of /opt/mydjangoapp/templates/) and wipe the reference at settings.TEMPLATES['DIRS'].

Upvotes: 3

Related Questions