【发布时间】:2020-07-28 15:58:44
【问题描述】:
这是我在 Django 中的模型:
from django.db import models
# Create your models here.
statusChoice = ((1, 'Show'), (2, 'Hide'))
class newCatagory(models.Model):
cat_name = models.CharField(max_length = 255)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank = True)
created_at = models.DateTimeField(auto_now_add=True)
status = models.SmallIntegerField(choices = statusChoice, null=True)
def __str__(self):
return "%s, --%s" % (self.parent, self.cat_name)
class products(models.Model):
pro_name = models.CharField(max_length = 255, null =True)
sub_cat_id = models.ForeignKey(newCatagory, on_delete=models.CASCADE, null=True, blank = True)
pro_image = models.ImageField(null = True)
pro_price = models.FloatField(null = True)
description = models.TextField(null = True)
status = models.SmallIntegerField(choices = statusChoice, null=True)
created_at = models.DateTimeField(auto_now_add=False, null=True)
def __str__(self):
return self.pro_name
我正在创建像上面这样的模型。但我不知道如何向我的观点展示我的类别。我该怎么做呢
这是我的模板 html:
<li><a href="#">{{This is catagory}}</a>
<ul class="sub-menu">
<li><a href="#">{{this is subcatagory}}</a></li>
</ul>
</li>
我正在尝试使用for循环但不知道如何使用,请帮助
【问题讨论】:
-
对不起,我不明白这篇文章:)
-
等一下
-
stackoverflow.com/questions/19193289/…。在这里参考没有第三方应用程序的替代解决方案他正在做的是'with'关键字,如果你有三层树,你可以像你一样进行递归,那么它也可以工作。