Search code examples
djangodjango-templatesdjango-settings

Standard for application-specific template directories in Django?


I guess this is a question related to best practises in Django development. I'm trying to build a web service with a main page (base.html) that contains multiple apps. I would like to make the apps self-contained, so I've made a templates directory in each app, and would also like to take advantage of the template inheritance feature of Django to make this whole thing as fluid as possible.

Now my concern is, where should I put the base.html in my project, so that the system knew where to find it? Also, what changes should I make in the settings.py file in order for the system to be able to connect the templates? Is there a standard or a known method that takes minimal effort for this sort of arrangement?


Solution

  • One common design pattern that I have both seen and used is to have a centralized "app" as a part of your project that contains all the shared "stuff" you care to use in other applications. So you might have the following directory structure:

    base/
      static/
        css/
          common.css
        js/
          common.js
      templates/
        base.html
    myapp1/
      urls.py
      views.py
      templates/
      ...
    myapp2/
      urls.py
      views.py
      templates/
      ...
    myproject/
      settings.py
      urls.py
    

    Now you just include the "base" application just like any other, and you put shared stuff inside it. Other applications can refer to templates that live there, and can include any common libraries that you may want to share.

    In settings.py:

    INSTALLED_APPS = ['base', 'myapp1', 'myapp2']