1đź‘Ť
Your directory structure seems strange…
First thing, in case the index.html
in the app directory is supposed to be a Django-template, it shouldn’t be under the static directory.
Also, you mentioned that you used the path /app/static/templates/index.html
, which actually shouldn’t work at all.
Usually in Django, the /static/
path will be used to access static resources from all apps’ static directories, as well as all directories specified in STATICFILES_DIRS
, as if all the content from all those directories is “merged” into one /static/
directory!
So, in your example, the path /static/templates/index.html
indeed refers to both the index.html
from the root project directory, as well as the index.html
from the app-specific static directory, which is why the actual file you get will depend on the order of static files finders specified.
The recommended layout to avoid such collisions would be:
-project root
-static
-global static resources accessible via /static/...
-app
-static
-app
-app-specific static resources accessible via /static/app/...
This is also appropriate for app-template directories:
-app1
-templates
-app1
-index.html (referred from Django view as 'app1/index.html')
-app2
-templates
-app2
-index.html (referred from Django view as 'app2/index.html')
Edit to add info on shared templates:
If you’re trying to have a “base-template” that is extended by other apps, I would recommend using the “common-app” approach.
You simply create a new app (named e.g. “common”, although you can name it however you want), that contains common templates (and other logic, if you’d like), and have the app-specific templates extend it.
The layout would be:
-app1
-templates
-app1
-index.html
-common
-templates
-common
-base.html
And in index.html
you’ll have {% extends "common/base.html" %}
at the top of the file (read the Django docs on template inheritance if you’re unfamiliar with it).
Of course, the common
app must be enabled in the Django settings for this to work.