views:

468

answers:

3

I'm working with a Satchmo installation that resides within an existing project. This project has its own templates as well as templates for some of the various apps that are installed. Some of these app-specific templates have their own app_base.html variations that expect to derive form base.html. I'd like to be able to do the same thing with my Satchmo templates and have them reside within my project's base, but also have some additional html added around all of them.

  • /templates
    • base.html
    • index.html
    • /news
      • news_base.html (extends base.html and adds news-specific template features)
      • index.html
      • detail.html
    • /store
      • base.html (overriding Satchmo's base)

This structure works somewhat, but not how I expected. in /store/base.html (Satchmo's base) I've simply replaced everything with a test message. I can see the message, so I know satchmo is loading its base and not the site's base. However, I can't extend my project's base anymore since using:

{% extends "base.html %}

Yields a recursion error since its calling itself and the following simply won't work.

{% extends "../base.html" %}

I realize that I can change my project's base.html to a slightly different name and point all app-specific templates at them, but it seems like a pretty major hack on such a fundamental aspect of the template structure.

A: 

Hmm, I didn't think django looked up templates relatively like that.

Kinda crazy hack, but this should work:

  • /templates/store/base.html extends "global_base.html"
  • /templates/global_base.html extends "base.html"
Matthew Marshall
A: 

Depending on how you have your template structure set up, it might also be a good idea to play with the settings.TEMPLATE_LOADERS variable.

TEMPLATE_LOADERS Default: ('django.template.loaders.filesystem.load_template_source', 'django.template.loaders.app_directories.load_template_source') A tuple of callables (as strings) that know how to import templates from various sources. See The Django template language: For Python programmers.

For more information on how this affects the template loading process: http://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types

From the way you describe your problem, it seems like by commenting out the "app_directories.load_template_source" file line, you might be able to better find a way to accomplish what you're doing.

django.template.loaders.app_directories.load_template_source Loads templates from Django apps on the filesystem. For each app in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django looks for templates in there.

This means you can store templates with your individual apps. This also makes it easy to distribute Django apps with default templates.

For example, for this setting:

INSTALLED_APPS = ('myproject.polls', 'myproject.music') ...then get_template('foo.html') will look for templates in these directories, in this order:

/path/to/myproject/polls/templates/foo.html /path/to/myproject/music/templates/foo.html Note that the loader performs an optimization when it is first imported: It caches a list of which INSTALLED_APPS packages have a templates subdirectory.

This loader is enabled by default.

Tom