Django-mptt将层次结构显示为表

时间:2018-07-25 13:32:45

标签: django hierarchy tabular django-mptt

我试图将层次表显示为普通表。

model.py

class Dimension_value(MPTTModel):
    name = models.CharField(max_length = 200, null=True, blank = True, default = '')
    parent = TreeForeignKey("self", on_delete=models.CASCADE, null=True, blank=True, related_name="children")

    class MPTTMeta:
        order_insertion_by = ['name']

    def __str__(self):
        return self.name

views.py

def show_genres(request):
    return render(request, "accounts/dimension_detail.html", {'dimensions': Dimension_value.objects.all()})

dimension_detail.html

<div class="container-fluid">

    <div class="card">
      <div class="card-body">
  <h2>My dimensions</h2>

  {% load mptt_tags %}

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

</div>

</div></div>

使用django-mptt,列表显示就很好。但是我不知道如何在普通表中显示它。

例如

  • 欧洲|荷兰|北荷兰省
  • 欧洲|荷兰|祖德-荷兰
  • 欧洲|比利时|弗兰德森

我现在能得到什么

<table>


    <thead>
        <tr>


            <th class="id orderable"><a href="?sort=id">ID</a></th>



            <th class="name orderable"><a href="?sort=name">Name</a></th>



            <th class="orderable parent"><a href="?sort=parent">Parent</a></th>



            <th class="asc lft orderable"><a href="?sort=-lft">Lft</a></th>



            <th class="orderable rght"><a href="?sort=rght">Rght</a></th>



            <th class="asc orderable tree_id"><a href="?sort=-tree_id">Tree Id</a></th>



            <th class="level orderable"><a href="?sort=level">Level</a></th>


        </tr>
    </thead>



    <tbody>


        <tr class="even">

                <td class="id">6</td>

                <td class="name">Asia</td>

                <td class="parent">—</td>

                <td class="lft">1</td>

                <td class="rght">4</td>

                <td class="tree_id">1</td>

                <td class="level">0</td>

        </tr>



        <tr class="odd">

                <td class="id">8</td>

                <td class="name">Japan</td>

                <td class="parent">Asia</td>

                <td class="lft">2</td>

                <td class="rght">3</td>

                <td class="tree_id">1</td>

                <td class="level">1</td>

        </tr>



        <tr class="even">

                <td class="id">5</td>

                <td class="name">Europe</td>

                <td class="parent">—</td>

                <td class="lft">1</td>

                <td class="rght">8</td>

                <td class="tree_id">2</td>

                <td class="level">0</td>

        </tr>



        <tr class="odd">

                <td class="id">7</td>

                <td class="name">Belgium</td>

                <td class="parent">Europe</td>

                <td class="lft">2</td>

                <td class="rght">7</td>

                <td class="tree_id">2</td>

                <td class="level">1</td>

        </tr>



        <tr class="even">

                <td class="id">9</td>

                <td class="name">Vlaanderen</td>

                <td class="parent">Belgium</td>

                <td class="lft">3</td>

                <td class="rght">4</td>

                <td class="tree_id">2</td>

                <td class="level">2</td>

        </tr>



        <tr class="odd">

                <td class="id">10</td>

                <td class="name">Wallonie</td>

                <td class="parent">Belgium</td>

                <td class="lft">5</td>

                <td class="rght">6</td>

                <td class="tree_id">2</td>

                <td class="level">2</td>

        </tr>


    </tbody>




</table>

0 个答案:

没有答案