【问题标题】:django-mptt not linking data together correctlydjango-mptt 没有正确地将数据链接在一起
【发布时间】:2011-05-24 07:46:27
【问题描述】:

嘿,我正在使用 MPTT 从包含对话的模型中创建一些树状数据,我希望它们按“投票”字段排序。

模型目前看起来是这样的,非常基础。

class Thread(MPTTModel):
    message = models.CharField(max_length=100)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
    votes = models.IntegerField()

    class MPTTMeta:
        order_insertion_by=['votes']

如您所见,我们有一个消息字段,以及链接到 Thread 模型的父 FK 和一个投票。

在我看来,我有这个

threads = Thread.tree.all()
    data = {
        'threads':threads
    }
    return render_to_response("show.html",data )

然后在我的模板中

{% load mptt_tags %}

<ul class="root">
        {% recursetree d %}
            <li>
                {{ node.title }}
                {% if not node.is_leaf_node %}
                    <ul class="children">
                        {{ children }}
                    </ul>
                {% endif %}
            </li>
        {% endrecursetree %}
</ul>

但是,输出的列表是所有线程的列表。它们都没有链接在一起。

有什么想法吗?

【问题讨论】:

  • 这是您的实际代码吗?您的视图不会在您的上下文中填充名为 d 的内容...

标签: python django mptt


【解决方案1】:
{% load mptt_tags %}
<ul class="root">
    {% recursetree nodes %}            
        <li>
            {{ node.message }}
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
                </ul>
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>

在我的观点中:

threads = Thread.tree.all()  
data = {  
    'nodes':threads  
}
return render_to_response("show.html",data )

在 html 页面中,我看到按字母顺序排列的节点树并带有缩进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多