Chris Muench
Chris Muench

Reputation: 18338

django override admin template

I am following part 2 of the Django tutorial. I am trying to override an admin template (base_site.html)

I copied the file from the django/contrib/admin/templates to mytemplates/admin/base_site.html

I also updated settings.py:

#Base Directory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))

#Template directories
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)

I tried putting the mytemplates folder in the root of the project folder as well as in the mysite folder with no luck. Any pointers would be great!

Upvotes: 7

Views: 12629

Answers (4)

3176243
3176243

Reputation: 569

@kat-russo, thx ;)

I tried to setup admin templates according to docs

project_name
-app1
-app2
-project_name //main folder -> settings.py , urls.py, wsgi.py
-templates
-admin
  -project_name
     base.html

without success, but

 -templates
   -admin
      base.html

works for me.

my config (Django 1.10.4 w/Django CMS 3.4.1)

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',
            'sekizai.context_processors.sekizai',
            'cms.context_processors.cms_settings',
        ],

    },
},
]

Upvotes: 1

erajuan
erajuan

Reputation: 2294

You can override all templates.

Create an admin directory in templates and add the files.

The all files.

https://github.com/django/django/tree/master/django/contrib/admin/templates/admin

Upvotes: 0

YardenST
YardenST

Reputation: 5266

EDITED PREVIOUS USER RESPONSE -- THIS WORKS:

I think your relative path to the templates directory is wrong.

If you follow these steps it should work: (I tested it myself)

  1. Put the mytemplates dir side by side with the manage.py file

    project
    -app1
    -app2
    -mytemplates
        -admin
            -base_site.html
    -manage.py
    
  2. Change the TEMPLATE_DIRS to:

    TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)
    
  3. Make sure the order of the template loader is:

    TEMPLATE_LOADERS = (
    
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    
    )
    

Upvotes: 13

Nimo
Nimo

Reputation: 8304

@YardenST's answer almost worked for me. I guess it's a matter of configuration.

In case you run into trouble, I suggest you use this line:

TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)

Next, put a breakpoint to show the actual outcome, or alternatively use print TEMPLATE_DIRS.

That's where you should place the templates you want to override.

Upvotes: 1

Related Questions