我想我正确地遵循了文档,但仍然出现了 le* 奇怪的问题:MPTT 模板标签很好地显示了树,缩进和全部,但不是 node.name 标记(空)
我的模特
class Forme(MPTTModel):
name = models.CharField(max_length=50, unique=True)
source_id = models.IntegerField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
def __unicode__(self):
return unicode(self.name)
class MPTTMeta:
order_insertion_by = ['name']
我的观点
def listnode(request):
nodes = Node.objects.using('hmdjango').all()
selectforme = Forme.tree.all()
allnodes = []
for n in Node.objects.using('hmdjango').all() :
try:
intervention = FieldDataFieldItemIntervention.objects.using('hmdjango').get(entity_id__exact=n.id)
except FieldDataFieldItemIntervention.DoesNotExist:
intervention.field_item_intervention_value = 'BOOM'
try:
forme = FieldDataFieldItemForme.objects.using('hmdjango').get(entity_id__exact=n.id)
except FieldDataFieldItemForme.DoesNotExist:
forme.field_item_forme_tid = 'BOOM'
allnodes.append({'id':n.id, 'title':n.title,\
'intervention':intervention.field_item_intervention_value, \
'forme':forme.field_item_forme_tid})
return render_to_response('node_list.html', {'nodes':allnodes, 'formes':selectforme}, context_instance=RequestContext(request))
我的模板(我确实加载了 mptt_tags)
<ul class="root">
{% recursetree formes %}
<li>
{{ forme.name }}
{% if not forme.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
编辑
通过“formes”变量的经典循环表明我可以访问名称属性。因此我的猜测是recursivetree
模板标签,由 django-mptt 提供,是我的主要嫌疑人
{% for forme in formes %}
<li>{{ forme.name }}
{% endfor %}