'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



Solution 1:[1]

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.

Solution 2:[2]

Supposing that you have a default project and app created as

  1. django-admin startproject xyz
  2. django-admin startapp abc
  3. both xyz and abc folders are in main project folder

Following minimal changes are required to get first template working

  1. in settings.py change INSTALLED_APPS to add 'abc' (I forgot and failed)
  2. in urls.py add
  • add line: import abc.views
  • change urlpatterns to add path('abc/', abc.views.index)
  1. in index definition of views.py use loader.get_template('abc/index.html')
  2. Create abc/templates/abc/index.html file ( I wrote "template" and spent few hours before I realized that is was "templates")
  3. make sure the templates and inside folders have execute permission and html file have read permission for "other" users. (I worked as root and failed due to this error)
  4. Restart the project (runserver or apache2 service)

Summary of my mistakes

  1. Edit INSTALLED_APPS
  2. "templates" is default
  3. setup x and r permissions

Solution 3:[3]

  1. Ensure your app is added to settings.py INSTALLED_APPS
  2. Ensure your config in apps.py has a name equal to the name of the application directory.
class DemoConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'app'  # this must be the same as the parent directory

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 scum