Opened 13 years ago
Closed 13 years ago
#20312 closed Bug (invalid)
Overriding template in app's templates directory
| Reported by: | Nikolay | Owned by: | nobody |
|---|---|---|---|
| Component: | Template system | Version: | 1.5 |
| Severity: | Normal | Keywords: | template, override |
| Cc: | bmispelon@… | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Django does not override templates placed in "<app>/templates" directory.
Maybe, "django.template.loaders.app_directories.Loader" works incorrectly?
Example:
There is a template "base_site.html" in django/contrib/admin/templates/admin/
In project we have app "admin" with "templates/admin/base_site.html"
We expect to see content of project's template but see Django's default.
(django/contrib/admin/templates/admin/base_site.html instead of project's).
In settings.py "django.template.loaders.filesystem.Loader" has greater priority then "app_directories.Loader" (default Django settings)
I created demo project (in attachment).
Follow this instruction to reproduce bug:
- syncdb to make django admin site working
- run development server
- open http://localhost:8000/admin/
You'll see normal login form with title "Django administration"
- Remove underscore from "override_demo/templates_" directory name
- Refresh page in browser.
You'll see title "MyOwnSite name" as expected.
Attachments (1)
Change History (2)
by , 13 years ago
| Attachment: | override_demo.tar.gz added |
|---|
comment:1 by , 13 years ago
| Cc: | added |
|---|---|
| Resolution: | → invalid |
| Status: | new → closed |
Hi,
From what I can tell, the demo you provided works as documented.
Templates are loaded using template loaders defined in
settings.TEMPLATE_LOADERS.Django goes through this list in order, which means that if you use the defaults,
filesystem.Loaderis first, followed byapp_directories.Loader.What
filesystem.Loaderdoes is that it goes through every directory insettings.TEMPLATE_DIRS(again, in order) and try to see if the template you ask for is in there.If the template is not found, it moves to the next directory in the list.
As for
app_directories.Loader, what it does is that it goes throughsettings.INSTALLED_APPS(in order too) and for each application in there, it tries to find atemplatesdirectory in the app folder and see if the required template is in there.If the requested template is found at any step, the process stop and that template is returned.
As you see, the order of execution is quite well defined and only depends on the order of your settings.
For your demo project, you see that
django.contrib.admincomes beforeoverride_demo.adminin yourINSTALLED_APPS, which explains why it takes precedence.I'm marking this ticket as
invalid. Feel free to reopen it if I misunderstood the issue you're describing.Thanks.