views:

459

answers:

1

The Django documentation states the following clearly:

Not every template in contrib\admin\templates\admin may be overridden per app or per model.

It then lists the ones that can, and base.html, base_site.html and index.html – the ones I'm interested in – are not among those listed. They can be overridden per-project, but not per-app.

My question is: is there a way around this that doesn't involve editing the code inside django.contrib.admin? I'm willing to consider some monkeypatching solutions :-). I really want my app to have custom versions of those three files inside its templates directory, and have each project that uses the app to use those.

The reason I'm interested is that I'm creating a large, reusable app with a heavily customized admin interface, and per-project overrides of the "core" templates aren't the best solution, since I'd have to copy the custom templates to the template directory of each project that the app gets used in. Releasing new versions of the app with new modifications to those core templates would mean re-copying everything to the affected projects. Ugh.

I understand the reasoning behind the decision to only make a select few templates overridable per-app; after all, if overriding them all was possible, which app's overridden admin would take precedence?

But in my case, the app will be the "centerpiece" of several client projects, with other apps in those projects merely being in a supporting role.

CSS-based customization of the existing templates only gets you so far, and I'm hesitant to rely on JavaScript DOM manipulation solutions unless absolutely necessary.

One solution that comes to mind is to place the custom base.html etc. templates inside appname/templates/admin/ and then symlink them to the project's templates folder. That way any updates to the app will automatically take effect on the project level.

Symlinking is probably my method of choice if nothing better is suggested, but I'd like to hear if anyone has a nicer solution.

+1  A: 

As I see your goal is to override templates for entire project, not for app or for model, but you don't want to put templates in project's template folder.

So you should just create 'base.html', etc. in 'your_app/templates/admin' folder. Next you have to tell django that templates should be loaded not only from project's template folder, but also from your app's folder. This can be done using TEMPLATES_DIR variable in settings.py file, smth. like that:

TEMPLATE_DIRS = (
    os.path.join(PROJECT_PATH, 'templates'),
    os.path.join(PROJECT_PATH, 'my_app','templates'),
)
Mike Korobov
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
Mike Korobov
My app doesn't live inside PROJECT_PATH, but otherwise that looks like a clean solution. Thanks!
JK Laiho