tree structure of parent child relation in django templates

前端 未结 4 2051
借酒劲吻你
借酒劲吻你 2021-01-31 12:40

how do i implement the tree structure in django templates with out using django-mptt.

i have model.

class Person(TimeStampedModel):
    name  = models.Ch         


        
4条回答
  •  梦毁少年i
    2021-01-31 13:18

    from Django while loop question and

    http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags

    # view.py
    
    @register.inclusion_tag('children.html')
    def children_tag(person):
        children = person.children.all()
        return {'children': children}
    
    # children.html
    
    
      {% for child in children %}
    • {{ child }}
    • {% if child.children.count > 0 %} {% children_list child %} {% endif %} {% endfor %}
    # your template {% children_tag parent %}

提交回复
热议问题