app template problem.

After writing the app template, when I look at the page that Django renders, templates/a.html is used, not app/templates/{app_name}/a.html.

I know that if app_dirs is true, the app template is searched first.
Do I need to configure anything else? Or am I mistaken?

If I remove templates/a.html, Django imports the app templates.

project/
   app/
       templates/
          {app_name}/
              a.html
   templates/
      a.html
TEMPLATES = [
    {
        'DIRS': [
            BASE_DIR / 'templates',
        ],
        'APP_DIRS': True,
    ...
    }
]

This statement is not accurate. Quoting from How to override templates:

… In other words, DIRS is searched before APP_DIRS.

While this is not directly on-point in this case since it’s talking about an override, it is accurate as far as the search order is concerned for templates.

This appears to be an artifact of the default definition used with the cached.Loader. (Also see Loading templates)

You can change this by supplying your own configuration for the loaders as shown in the example by reversing the order of the two default loaders.
e.g:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ BASE_DIR / 'templates' ],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                (
                    'django.template.loaders.cached.Loader',
                    [
                        'django.template.loaders.app_directories.Loader',
                        'django.template.loaders.filesystem.Loader'
                    ]
                )
            ]
        },
    },
]

You can check the following information on the page you linked:

With APP_DIRS set to True, the template loader will look in the app’s templates directory and find the templates.

It will do that by default, assuming it wasn’t found in the directories defined by the DIRS setting. But it does search DIRS first by default.

I’m not sure I understand what point you’re trying to make here, since you wrote this post having already discovered this to be true.

1 Like

the template loader will look in the app’s templates directory and find the templates.

What it means is

  1. Search the app template first, then
  2. Search the template dirs.

Isn’t this correct?
But the Django do reversed.

Sir, that is not what @KenWhitesell meant.

If I understand correctly, Django will first search /project/templates before searching /project/app/templates.

Since you have the following in settings.py:
'DIRS': [ BASE_DIR / 'templates' ],

That means DIRS is /project/templates. The first matching template in DIRS will apply. If Django finds none in DIRS, it will search all apps in /project for a match (hence APP_DIRS).

1 Like

Oh, I understand.
Then I guess I knew the opposite.