[Answered ]-Where are template tags include files expected to be found in Django framework

1👍

According to the include documentation:

The template name can either be a variable or a hard-coded (quoted)
string, in either single or double quotes.

Put the template name into quotes:

{% include "xy.html" %}

FYI, xy.html should be anywhere inside TEMPLATE_DIRS directories.

FYI2, setting TEMPLATE_DEBUG setting to True would help to see the detailed error message in case of an exception raised while rendering the template.

👤alecxe

1👍

Let’s use an example project called foo:

foo
    -blog
        -admin.py
        -models.py
        -views.py
        -tests.py
        -templates
            -blog
                -post.html
    -news
        -admin.py
        -models.py
        -views.py
        -tests.py
        -templates
            -newsitem.html
    -foo
        -settings.py
        -urls.py
        -wsgi.py
    -templates
        -base.html
        -blog
            -post.html

If your settings.py includes:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates')
)

Then Django overrides app templates with the TEMPLATE_DIRS template, this means that if a post.html resides both in blog/templates/blog/post.html and templates/blog/post.html (as the above example)
then Django will load the later.
If you don’t specify a TEMPLATE_DIRS then Django searches for the post.html within the templates folder of each app, this means that if you specify a: blog/templates/blog/post.html and a news/templates/blog/post.html both are valid,
in this occasion Django will load the template depending on how you INSTALLED_APPS looks like:

INSTALLED_APPS = (

    ...
    'blog',
    'news',
    ...
)

Then Django will render the template of the blog app instead of the template of news app (if we had a blog/post.html under the templates of news).
Yet if your INSTALLED_APPS looks like:

INSTALLED_APPS = (
    ...
    'news',
    'blog',
    ...
)

Then Django will load post.html from the news app rather than blog app (again if we had templates/blog/post.html under our news app).
You should also be picky about template names, templates with similar names will override each other depending on your settings and INSTALLED_APPS order (higher overrides lower), TEMPLATE_DIRS templates always override all others.

Leave a comment