【发布时间】:2017-02-17 02:25:30
【问题描述】:
我有以下代码:
{% if thing.parent.parent.parent.link %}
<a href="/y/{{ thing.parent.parent.parent.link }}">{{ thing.parent.parent.parent.name }}</a>
{% endif %}
{% if thing.parent.parent.link %}
<a href="/y/{{ thing.parent.parent.link }}">{{ thing.parent.parent.name }}</a>
{% endif %}
{% if thing.parent.link %}
<a href="/y/{{ thing.parent.link }}">{{ thing.parent.name }}</a>
{% endif %}
在这个非递归代码中改变的关键是在链接/链接文本中添加一个 .parent。
我总共需要这样做 20 次,如您所见,重复非常难看而且非常重复。使用 django 的模板框架有没有更递归更好的方法?
【问题讨论】:
标签: python django recursion django-templates