简体   繁体   English

Django递归模板渲染期间的运行时错误

[英]Runtime error during django recursive template rendering

I want to implement django recursive template rendering for getting users and their subordinates. 我想实现django递归模板渲染,以获取用户及其下属。 However, I've got a ErrorRuntimeError at /admin/users/3 maximum recursion depth exceeded in instancecheck . 但是,我在instancecheck中超出了/ admin / users / 3最大递归深度的ErrorRuntimeError。 I'm using django 1.9. 我正在使用django 1.9。 The code is given below 代码如下

The input is like that: [{name: 'user1@mail.com', id: 1, next: True}, {name: 'user2@mail.com', id: 2, next: True}, {name: 'user3@mail.com', id: 3, next: False}] 输入是这样的: [{name: 'user1@mail.com', id: 1, next: True}, {name: 'user2@mail.com', id: 2, next: True}, {name: 'user3@mail.com', id: 3, next: False}]

users_hierarchy.html users_hierarchy.html

<ul>
{% for user in users %}
    <li>{{ user.name }}</li>
    {% if user.next %}
        <ul>
           {% include 'users/user_hierarchy.html' with data=user %}
        </ul>
    {% endif %}
{% endfor %}

I expect a html such as: 我期望一个HTML如:

<ul>
<li>user1@mail.com
    <ul>
        <li>user2@mail.com
    <ul>
         <li>user3@mail.com</li>
    </ul>
        </li>  
    </ul>
</li>

What I am doing wrong? 我做错了什么?

I explain why you are in an infinite recursion 我解释了为什么您要无限递归

From djanto include template docs : djanto包含模板文档

An included template is rendered within the context of the template that includes it. 包含的模板在包含它的模板的上下文中呈现。

That means you are iterating over users again and again: 这意味着您要一次又一次地遍历users

<ul>
{% for user in users %}                 <-----(1)<------------------------<
    <li>{{ user.name }}</li>                                              |
    {% if user.next %}                                                    |
        <ul>                                                              |
           {% include 'users/user_hierarchy.html' with data=user %} --(2)->

At (1) you iterate over users . 在(1),您遍历users At first iteration you call the include (2). 第一次迭代时,您将调用include (2)。 At this point you go to (1) (in nested included template) and start iteration again. 此时,您转到(1)(在嵌套的包含模板中)并再次开始迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM