Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.template.loaders.app_directories.Loader fails to find the template file

The template file is saved under the app directory, but it raises TemplateDoesNotExist exception while rendering:

Template-loader postmortem as following:

Django tried loading these templates, in this order:

Using loader django.template.loaders.app_directories.Loader:
    ...
    $PROJECT/apps/myapp/templates/search.html (File does not exist)
    ...

I'm wondering why it looks for:

$PROJECT/apps/myapp/templates/search.html

rather than:

$PROJECT/apps/myapp/templates/myapp/search.html

The latter does exist indeed

like image 610
vts Avatar asked Jun 17 '13 06:06

vts


2 Answers

$PROJECT/apps/myapp/templates/search.html. That is the path it'll look for as the doc says.

django.template.loaders.app_directories.Loader will look for a templates directory inside all the INSTALLED_APPS in order.

like image 149
Babu Avatar answered Oct 23 '22 16:10

Babu


django.template.loaders.filesystem.load_template_source: This loader loads templates from the filesystem, according to TEMPLATE_DIRS. It is enabled by default.

django.template.loaders.app_directories.load_template_source: This loader loads templates from Django applications on the filesystem. For each application in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django looks for templates there.

This means you can store templates with your individual applications, making it easy to distribute Django applications with default templates. For example, if INSTALLED_APPS contains ('myproject.polls', 'myproject.music'), then get_template('foo.html') will look for templates in this order:

/path/to/myproject/polls/templates/foo.html
/path/to/myproject/music/templates/foo.html

Note that the loader performs an optimization when it is first imported: it caches a list of which INSTALLED_APPS packages have a templates subdirectory.

This loader is enabled by default.

like image 32
GrvTyagi Avatar answered Oct 23 '22 15:10

GrvTyagi