7

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!

jcern
  • 7,798
  • 4
  • 39
  • 47
Chris Muench
  • 17,444
  • 70
  • 209
  • 362

4 Answers4

13

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',
    
    )
    
Kat Russo
  • 469
  • 4
  • 8
YardenST
  • 5,119
  • 2
  • 33
  • 54
  • 2
    Shouldn't `admin` be a subdirectory to `mytemplates/` with this configuration? – bnjmn Jan 26 '14 at 19:26
  • this works, as long as you use the correct folder name in `TEMPLATE_DIRS = (os.path.join ...` – Don Cheadle Feb 02 '15 at 21:01
  • it's confusing because I've seen people specify templates within the app, within the project, and now here -- at the project-base? I don't even know what to call this level – Don Cheadle Feb 02 '15 at 21:02
1

@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.

Nimo
  • 7,984
  • 5
  • 39
  • 41
1

@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',
        ],

    },
},
]
3176243
  • 561
  • 4
  • 7
0

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

erajuan
  • 2,224
  • 20
  • 31