[Solved]-Django.template.loaders.app_directories.Loader fails to find the template file

8👍

$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.

👤Babu

4👍

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.

1👍

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

0👍

  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
👤scum

-3👍

Add them to your settings file.

    import os
    PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

    TEMPLATE_DIRS = (
        os.path.join(PROJECT_ROOT, "/apps/myapp/templates/"),
    )
👤cem-

Leave a comment