Opened 10 years ago

Closed 10 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


Django does not override templates placed in "<app>/templates" directory.
Maybe, "django.template.loaders.app_directories.Loader" works incorrectly?

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 "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:

  1. syncdb to make django admin site working
  2. run development server
  3. open http://localhost:8000/admin/

You'll see normal login form with title "Django administration"

  1. Remove underscore from "override_demo/templates_" directory name
  2. Refresh page in browser.

You'll see title "MyOwnSite name" as expected.

Attachments (1)

override_demo.tar.gz (4.6 KB) - added by Nikolay 10 years ago.

Download all attachments as: .zip

Change History (2)

Changed 10 years ago by Nikolay

Attachment: override_demo.tar.gz added

comment:1 Changed 10 years ago by Baptiste Mispelon

Cc: bmispelon@… added
Resolution: invalid
Status: newclosed


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.Loader is first, followed by app_directories.Loader.

What filesystem.Loader does is that it goes through every directory in settings.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 through settings.INSTALLED_APPS (in order too) and for each application in there, it tries to find a templates directory 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.admin comes before override_demo.admin in your INSTALLED_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.


Note: See TracTickets for help on using tickets.
Back to Top