如何在Django模板中以所需格式加载此类别?

时间:2013-12-01 11:35:46

标签: django django-templates

我有Category这样的模型:

class Category(models.Model):    
    name = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    parent = models.ForeignKey('self', blank=True, null=True, related_name="children")
    description = HTMLField(blank=True, null=True)

我希望将它们列为这样:

导航栏中没有父级的类别如下:

Category.objects.filter(parent=None)

让我们说父== SuperParent。

现在我想在小部件中加载其他类似父类的类别:

Widget Box 1:

Category Parent (children of SuperParent):
   Child1 of parent 
   Child2 of parent 
   .. and so on

Widget Box 2

Category Parent(another name)(Children of SuperParent):
    Childs

怎么做?

由于

1 个答案:

答案 0 :(得分:0)

  

p_cat = Category.objects.filter(parent = None)

<强>模板

  

{%for cat in p_cat%}

     

{{cat.name}}

     

{%表示cat.children.all%中的ch_cat}

     
  • {{ch_cat.name}}
     

{%endfor%}

     

{%endfor%}