Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin template override not working

Django 1.6.11

App structure looks like:

my_project/
     |-- new_app/
     |-- templates/

in my config:

TEMPLATE_ROOT = os.path.join(BASE_ROOT, 'templates/')
TEMPLATE_DIRS = (
    TEMPLATE_ROOT,
)
INSTALLED_APPS = (
    'django.contrib.admin',
    ...
    'new_app',
)

I've also tried listing new_app before contrib.admin and that didn't help.

When I copy venv/django/contrib/admin/templates/admin/change_list.html to my /templates/admin/new_app/change_list.html I don't see my customizations show up.

my_project/
     |-- new_app/
     |-- templates/
         |-- admin/
             |-- new_app/
                 |-- change_list.html

When I move change_list.html up one level so it's under the admin path, the changes show up just fine:

my_project/
     |-- new_app/
     |-- templates/
         |-- admin/
             |-- change_list.html
             |-- new_app/   (now an empty folder)

... but of course that would mean my changes are going to affect every admin page, not just for the app I'm trying to modify.

I've added this to the app's only model within admin.py:

class MyModelAdmin(reversion.VersionAdmin):
    change_list_template = 'admin/new_app/change_list.html'

... this gives me some of what I need, but I also need change_list_results.html and there's no ModelAdmin override for that.

I'm following the documentation guide found at readthedocs in section 2.4.8 on page 31, but I don't seem to be having any luck.

like image 320
iandouglas Avatar asked Oct 10 '16 18:10

iandouglas


People also ask

How do I override a Django template?

To override these templates, you will need to have an admin folder in your templates folder. If you do not have a templates folder, you can create it in the main project folder. To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly.

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.

What is App_dirs in Django?

With APP_DIRS set to True , the template loader will look in the app's templates directory and find the templates.

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/ ).


1 Answers

When several applications provide different versions of the same resource (template, static file, management command, translation), the application listed first in INSTALLED_APPS has precedence. See docs.

Change:

INSTALLED_APPS = (
    'django.contrib.admin',
    ...
    'new_app',
)

To:

INSTALLED_APPS = (
    'new_app',
    'django.contrib.admin',
    ...
)

Your templates in new_app should now be found before the templates in contrib.admin.

like image 150
allcaps Avatar answered Nov 12 '22 12:11

allcaps