Philipp S.
Philipp S.

Reputation: 981

Django; How to debug {% include '' %} tag within templates

Sometimes I get an empty string returned for my include tags which is impossible, because I have some static elements in it.

This is happening sometimes in my productive environment. How can I debug such an issue?

For example my footer or header disappears in some cases (Which I include within my "base.html") which I can't reproduce.

Thx

Example:

base.html

<html>
... 
{% include "subdir/_header.html" %}
...
{% block content %}
    <h1>Default Content</h1>
{% endblock %}

...
{% include "subdir/_footer.html" %}
</html>

home.html

{% extends "base.html" %}

{% block content %}
   <h1>Home related Content</h1>
{% endblock %}

So if I load home.html my footer sometimes disappears. No idea why, no errors.

FOUND THE PROBLEM

A missing static file caused that kind of problem. Unfortunately I did not get an error.

Upvotes: 0

Views: 530

Answers (2)

Diego Vin&#237;cius
Diego Vin&#237;cius

Reputation: 2223

Your base html should be Extended and not Included... include is for small blocks of codes, like components... blog post, news, contact and that stuffs

base html

{% load static %}

<!DOCTYPE html>
<html lang="en-US">

{% include 'path/head.html' %}

{% block content %}{% endblock %}

{% include 'path/footer.html' %}

</html>

other pages

{% extends 'base.html' %}
{% load static %}

{% block content %}
<!-- Content from each page -->
{% endblock %}

Obs.: Include inside of includes dont work propely, you can get the data from first page in includes inside includes... for that i usualy use templatetags or context_processors, depending of your need.

Use django-debug-toolbar to debug your entire application in dev environment, you can see the entire request and the includes that you want

https://django-debug-toolbar.readthedocs.io/en/stable/

Upvotes: 0

uerden
uerden

Reputation: 185

I don't know the exact reason of this issue, but can you try with {% extends 'base.html' %}.

The difference between include and extends right here:

{% include %} vs {% extends %} in django templates

Upvotes: 1

Related Questions