While setting up my project and working to keep apps non-dependent, I've hit a snag. I'd like all of the templates from the different apps to have a consistent header and footer. Here's what I'm trying:
myproject/ base/ templates/ header.html footer.html app1/ templates/ my_app1_page.html -> want to include 'header.html' and 'footer.html' from base app
Pretend there are many more apps that want to do this as well. Is this possible and/or the right way to do it?
As long as the apps are in INSTALLED_APPS and the template loader for apps dirs is enabled, you can include any template from another app, i.e.:
{% include "header.html" %}
... since your templates are located directly in the templates dir of your app. Generally, in order to avoid name clashes it is better to use:
app1/ templates/ app1/ page1.html page2.html app2/ templates/ app2/ page1.html page2.html
And {% include "app1/page1.html" %}
or {% include "app2/page1.html" %}
...
But: for keeping a consistent look and feel, it is so much better to use template inheritance rather than inclusion. Template inheritance is one of the really good things of the Django template system, choose inheritance over inclusion whenever it makes sense (most of the time).
My recommendations:
{%block content%}
for your main content.{% extends "base.html" %}
and override the content sectionSee another response to this question for links to the doc
While you can certainly do that by using the include tag and specifying absolute paths, the proper way to work in Django is by using Template inheritance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With