disruptive
disruptive

Reputation: 5946

Where are template tags include files expected to be found in Django framework

I'm having trouble including within a template thats it. I cannot find in the documentation where the include goes to look for the included .html (I assumed it would be the same as the templates?), I'm putting {% include "xy.html" %} in one file, but it looks like xy.html is not being found. I can't find any error returned either. xy.html is in the same directory as its calling file which is in a template folder in Django parlance.

I get no error message displayed, I simply dont get the included file displayed.

Where should I place the xy.html file?

Upvotes: 1

Views: 109

Answers (2)

petkostas
petkostas

Reputation: 7460

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.

Upvotes: 1

alecxe
alecxe

Reputation: 473903

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.

Upvotes: 1

Related Questions