[Fixed]-Include template tag not working in django

20👍

I had the same problem,but i will answer this question in a way that other users with a similar problem can understand.

you probably have a template block of some kind in the included html file,
this block either is expecting an include of some kind or is causing an error of which is calling exception that django passes and therefore you are not able to see the error,
if you use {% load someLoad %} in the parent template then use it in the included html too,
i guess this changes from version to version.

in my (very specific) case i had this missing in the included html file:

{% load i18n %} 
{% load cms_tags sekizai_tags %}

13👍

Another situation where an include statement will fail without raising an error is if you are working in a template which extends another, and your include is outside of a named block:

{% extends "my_base.html" %}

{% block content %}
    {{ block.super }}
    {% include "partials/file1.html" %}
{% endblock %}

{% include "partials/file2.html" %}

In this case file2.html will not be included because it is not in a block, and you will get no warning messages, and will try all sorts of things before you realise what you’ve done 🙂

5👍

I just had a similar problem. I was able to diagnose it by temporarily copying and pasting the html (i.e, navigation.html) that I wanted to include directly into the parent page (base.html). It seems that if there are errors in the included html, it just doesn’t get read in and no errors appear.

With the code from navigation.html pasted into base.html I got a 500 error because one of the named urls in navigation.html had no reverse.

3👍

If there is any issue/ error with the include template, django by default won’t show the error. You can enable it using the setting variable TEMPLATE_DEBUG = True Then you will able to view the error in the template when you try to load that page.

In the above case there may be some load tag is missing {% load xxx %} in the included template.

0👍

if you don’t want to mess with template tags, load and include options, there is another way to do this:

render the template explicitly with your parameter, store it as a string, then render into the main template as a context.

from django.template.loader import render_to_string
    def get_context_data(self, **kwargs):
        ctx['loader'] = render_to_string('common/loader.html', {'loader': randint(0, 15)})
        return ctx

0👍

i have the same issue, and i try all the method upon. But still have no include the "html". Then, i try to move the "html" from the app/template to the project/template. it’s worked. so i move the html file back, and add the path in setting.py about the template path. it’s solved. Even you didn’t add the template path in setting.py, the pycharm still can link the "including file " to the exact place. and run the django project without error….it took me 3days…

👤Vince

0👍

You have a syntax error because you wrote

{% include archivive_name %}

instead of

{% include 'archive_name.html' %}

which has the .html and the enclosing ''

Leave a comment