首页 > 解决方案 > Django:包含标签后出现严重错误

问题描述

在我在 django 模板中使用包含标签后,我得到了错误。

错误:

RecursionError at /home/
maximum recursion depth exceeded
Error during template rendering

我的 base.html 文件。

<html>
{% load static %}
<some static css here>
{% include "inc/header.html" %}
     {% block header %} HEADER {% endblock header %}
<some static js here>
</html>

我的头文件是

{% extends 'base.html' %}
<p> test header</p>

标签: djangodjango-templatesdjango-tagging

解决方案


您现在正在同时做两件事,您正在扩展“base.html”文件,同时您正在包含“header.html”。

这就是为什么你会遇到递归问题。

为了使事情正常工作,您需要删除 {% include %} 代码块。所以你的头文件看起来像

{% extends 'base.html' %}
{% block header %}
<p> Test Header </p>
{% endblock %}

虽然你的 base.html 不需要'HEADER' beetwen 你的 {% block header %} 标签,所以它应该看起来像:

<html>
{% load static %}
<some static css here>
{% block header %}{% endblock %}
<some static js here>
</html>

推荐阅读