题
我的内存中有一个树结构,我想使用 Django 模板以 HTML 形式呈现它。
class Node():
name = "node name"
children = []
会有一些物体 root
这是一个 Node
, , 和 children
是一个列表 Node
s。 root
将传入模板的内容。
我已经发现 这 关于如何实现这一点的讨论,但海报表明这在生产环境中可能不太好。
有人知道更好的方法吗?
解决方案
我认为规范的答案是:“不”。
相反,你应该做的是解开你内心的事情 看法 代码,因此只需迭代模板中的(in|de)凹痕即可。我想我会通过在递归遍历树的同时将缩进和缩进添加到列表中,然后将该“游记”列表发送到模板来做到这一点。(然后模板将插入 <li>
和 </li>
从该列表中,通过“理解”它来创建递归结构。)
我也很确定递归地包含模板文件确实是一个 错误的 办法...
其他提示
使用 with
模板标签,我可以做树/递归列表。
示例代码:
主要模板:假设“all_root_elems”是一个或多个树根的列表
<ul>
{%for node in all_root_elems %}
{%include "tree_view_template.html" %}
{%endfor%}
</ul>
tree_view_template.html 呈现嵌套 ul
, li
和用途 node
模板变量如下:
<li> {{node.name}}
{%if node.has_childs %}
<ul>
{%for ch in node.all_childs %}
{%with node=ch template_name="tree_view_template.html" %}
{%include template_name%}
{%endwith%}
{%endfor%}
</ul>
{%endif%}
</li>
这可能比您需要的要多得多,但是有一个名为“mptt”的 django 模块 - 它在 sql 数据库中存储分层树结构,并包含用于在视图代码中显示的模板。你也许能在那里找到有用的东西。
这是链接: Django-mptt
我为时已晚)你们所有人都使用了很多不必要的 和 标签,这就是我的恢复方式:
在主模板中:
<!-- lets say that menu_list is already defined -->
<ul>
{% include "menu.html" %}
</ul>
然后在menu.html中:
{% for menu in menu_list %}
<li>
{{ menu.name }}
{% if menu.submenus|length %}
<ul>
{% include "menu.html" with menu_list=menu.submenus %}
</ul>
{% endif %}
</li>
{% endfor %}
是的,你可以做到。这是一个小技巧,将文件名传递到{%include%}作为变量:
{% with template_name="file/to_include.html" %}
{% include template_name %}
{% endwith %}
Django 有一个内置的模板助手来应对这种情况:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#unordered-list
我遇到了同样的问题,我写了一个模板标签。我知道还有其他类似的标签,但无论如何我需要学习制作自定义标签:)我认为结果非常好。
阅读文档字符串以获取使用说明。
没有人喜欢听写吗?我可能在这里遗漏了一些东西,但这似乎是设置菜单的最自然的方式。使用键作为条目,使用值作为链接,将其弹出到 DIV/NAV 中,然后就可以走了!
从你的基地
# Base.html
<nav>
{% with dict=contents template="treedict.html" %}
{% include template %}
{% endwith %}
<nav>
称之为
# TreeDict.html
<ul>
{% for key,val in dict.items %}
{% if val.items %}
<li>{{ key }}</li>
{%with dict=val template="treedict.html" %}
{%include template%}
{%endwith%}
{% else %}
<li><a href="{{ val }}">{{ key }}</a></li>
{% endif %}
{% endfor %}
</ul>
它还没有尝试过默认的或订购的,也许你已经尝试过了?
纠正这个:
root_comment.html
{% extends 'students/base.html' %}
{% load i18n %}
{% load static from staticfiles %}
{% block content %}
<ul>
{% for comment in comments %}
{% if not comment.parent %} ## add this ligic
{% include "comment/tree_comment.html" %}
{% endif %}
{% endfor %}
</ul>
{% endblock %}
树评论.html
<li>{{ comment.text }}
{%if comment.children %}
<ul>
{% for ch in comment.children.get_queryset %} # related_name in model
{% with comment=ch template_name="comment/tree_comment.html" %}
{% include template_name %}
{% endwith %}
{% endfor %}
</ul>
{% endif %}
</li>
例如 - 型号:
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
# Create your models here.
class Comment(models.Model):
class Meta(object):
verbose_name = _('Comment')
verbose_name_plural = _('Comments')
parent = models.ForeignKey(
'self',
on_delete=models.CASCADE,
parent_link=True,
related_name='children',
null=True,
blank=True)
text = models.TextField(
max_length=2000,
help_text=_('Please, your Comment'),
verbose_name=_('Comment'),
blank=True)
public_date = models.DateTimeField(
auto_now_add=True)
correct_date = models.DateTimeField(
auto_now=True)
author = models.ForeignKey(User)
我遇到了类似的问题,但是我首先使用 JavaScript 实现了该解决方案,然后才考虑如何在 django 模板中完成同样的事情。
我使用序列化程序实用程序将模型列表转换为 json,并使用 json 数据作为层次结构的基础。