Django conditional include tag

I came across a very strange behavior of the Django template system. I have a template file, namely test.htmlone that recursively includes:

{% include "test.html" %}

Of course, such a template has no chance of being rendered, since there is no final condition. Ok, try the following:

{% if test_false %}{% include "test.html" %}{% endif %},

where test_falseis the variable passed to the template and equal False.

It is expected that it simply does not include anything, but it does:

RuntimeError at /test/
maximum recursion depth exceeded while calling a Python object

I do not understand. Include tags can take arguments from the current context, so I doubt that it runs before any other part of the page. Then why does it ignore the condition tag?

+5
source share
2 answers

Django , , .

:

{% include test_template %}

Django , .

+6

, Django , - :

{% include "test.html" %}

Python.

, - with:

{% with "test.html" as path %}
    {% include path %}
{% endwith %}
-1

All Articles