Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django override admin template

Tags:

django

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!

like image 231
Chris Muench Avatar asked Feb 07 '13 15:02

Chris Muench


People also ask

How do I override a Django template?

You can either put template overrides in your project's templates directory or in an application's templates directory. If you have app and project templates directories that both contain overrides, the default Django template loader will try to load the template from the project-level directory first.

Can we change Django admin theme?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

Can we customize Django admin template?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

Where are Django admin templates stored?

The default templates used by the Django admin are located under the /django/contrib/admin/templates/ directory of your Django installation inside your operating system's or virtual env Python environment (e.g. <virtual_env_directory>/lib/python3. 5/site-packages/django/contrib/admin/templates/ ).


2 Answers

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',
    
    )
    
like image 61
YardenST Avatar answered Sep 29 '22 12:09

YardenST


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

like image 30
Nimo Avatar answered Sep 29 '22 11:09

Nimo