load django templates according to app -
problem
according django documentation, template loader 'django.template.loaders.app_directories.loader' loads templates searching in installed_apps and
... , use 1 finds first.
however, i'm struggling understand why consistent django's spirit of having self-contained apps. let's take @ example.
example
consider using django-"preferable" approach of templates subdirectories.
root/ -manage.py -main/settings.py -main/urls.py -main/views.py -main/templates/base.html -main/templates/base/header.html -main/templates/base/secondary_header.html -main/app1/base_extension.html -main/app1/base/secondary_header.html
naturally init.py in main/ , main/app1/.
since i'm using standard template directory name, need use app_directories.loader in template_loaders.
consider following base.html , base_extension.html:
#base.html {% include "header.html" %} {% block secondary_header %}{% include "base/secondary_header.html" %}{% endblock %} #base_extension.html {% extends "base.html" %} {% block secondary_header %}{% include "base/secondary_header.html" %}{% endblock %}
in spirit of django, expect if base.html rendered, uses main's secondary_header (main/templates/base/header.html), if base_extension.html rendered, secondary_header.html should specific 1 app1 (main/app1/base/secondary_header.html). not happens due quoted text above.
this issue can dangerous, pointed out in django documentation
the order of installed_apps significant!
as installed app on top (in installed_apps) of app of main page of website can "overload" templates if there template name collision.
this makes django's app system little "odd", since each app must have templates don't collide others, if programer knows, due hierarchic structure of directories, templates belong app.
question
is there way solve issue? issue discussed in django community or there loader around solves this?
simply prefix app templates app's name. best practice in many ways, , solve problem well. use following structure:
project/app1/templates/app1__base.html project/app1/templates/app1__header.html project/app1/templates/app1_main.html project/templates/_base.html project/templates/_header.html project/app2/templates/app2__base.html
Comments
Post a Comment