Question

Which layout is better(Django 1.4+),:

project/templates/base.html
# project/templates/app/base.html # overriding some_app at project level, optional
project/app/templates/app/base_app.html
project/app/templates/app/base_app_index.html
  • base_app_index.html extends base_app.html -- app level
  • base_app.html extends base.html -- project level.

or usable like this(no share between apps with one project)

project/app/templates/app/base_app.html
project/app/templates/app/base_app_index.html
  • just base_app_index.html extends base_app.html

References:

From Django Docs:

  • Create a base.html template that holds the main look-and-feel of your site.
  • Create a base_SECTIONNAME.html template for each “section” of your site. For example, base_news.html, base_sports.html. These templates all extend base.html and include section-specific styles/design.
  • Create individual templates for each type of page, such as a news article or blog entry. These templates extend the appropriate section template.

From Django Best Pratices:

Location Templates typically live in one of two places, inside the application or at the root level of a project. We recommend keeping all your templates in the project template directory unless you plan on including your application in multiple projects (or developing it as a open source “reusable” application). In that case, it can be helpful to ship with a set of sample templates in the application, allowing it to work out-of-the-box or serving as an example for other developers.

From Two Scoops of Django:

  • Each app has a base_.html template. App-level base templates share a common parent, base.html.
  • Templates within apps share a common parent base_.html template.
  • Any template at the same level as base.html inherits base.html
Was it helpful?

Solution

It likely depends on your purposes. For most purposes, the first example will work best. Your base.html will include a lot of site-specific stuff that you won't want to repeat across each and every base_app.html template.

If you are making reusable apps that you want to share with other people, then it's probably best to use the second example. This way someone that uses your app won't need a base.html template to get your app up and running. It's a little more "all-inclusive".

This is basically what those docs are saying. The general advice is use the first method unless you're open-sourcing it and putting it on PyPi or something.

Even if you want to make a package reusable for your use only (i.e. use in multiple projects you're working on), I'd go with the first method. You can make it so you always have a base.html in your template directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top