如何在渲染期间将子类别嵌套在父类别下



我希望将每个类别对象的子类别呈现为嵌套在其父类别中的下拉菜单。 我很难实现这一点。它是如何工作的?

这是我的类别和子类别模型:

class Category(models.Model):
name = models.CharField(max_length=32)

class SubCategory(models.Model):
name = models.CharField(max_length=50)
category = models.ForeignKey(Category, on_delete=models.CASCADE)

这是我的视图函数:

def home(request):
categories = Category.objects.all()
#sub_categories = I don't know how
context = {
'categories': categories,
#'sub_categories': sub_categories
}
return render(request, 'blog/home.html', context)

这是在我的模板中:

{% for category in categories %}
<div class="nav-item dropdown">
<a href="#" class="py-3 nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ category.name }}
</a>
<div class="dropdown-menu rounded-0">
{% for sub_category in sub_categories %}
<a class="dropdown-item" href="#">
{{ sub_category.name }}
</a>
{% endfor %}
</div>
</div>
{% endfor %}

类别渲染良好,我的问题是子类别。

您的sub_categories模型需要以某种方式链接到类别模型。 在你的sub_categories模型中应该有这样的东西:

class Sub_categories(models.Model):    
category = models.OneToOneField(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)

您可以在 views.py 执行此操作:

def home(request):
categories = Category.objects.all().order_by('-date_posted')
context = {
'categories': categories,
}
return render(request, 'blog/home.html', context)

在您的模板中,您可以执行此操作:

{% for category in categories %}
<div class="nav-item dropdown">
<a href="#" class="py-3 nav-link dropdown-toggle" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{ category.name }}
</a>
<div class="dropdown-menu rounded-0">
{% for sub_category in category.sub_categories.all %}
<a class="dropdown-item" href="#">
{{ sub_category.name }}
</a>
{% endfor %}
</div>
</div>
{% endfor %}

我假设您也可以在类别模型中使用一个 ManyToManyField 来使类别模型与sub_category模型相关。

此方法不需要对模型进行任何更改。

在这种情况下,我必须做的是将类别和子类别放在一个单一的数据结构中,然后将整个内容传递给模板。

这是我完成的另一个项目的一些代码。我正在将一项研究与其参与者名单配对。

views.py

participants = []
try:
studies = Study.objects.filter(investigators=Researcher.objects.get(user_id=request.user.id))
except (Study.DoesNotExist, Researcher.DoesNotExist) as e:
studies = []
participantList = []
for study in studies:
participantList = Approval.objects.filter(study=study)
if (len(participantList) == 0 ):
participants.append([[], study])
elif (len(participantList) == 1 ):
participants.append([participantList, study])
return render(request, 'manage.html',{'studies':participants, 'profile': user_profile})

模板.html

{% for participants, study in studies %}
{{study.title}}
{% for part in participants %}
<p>{{part.info}}</p>
{% endfor %}
{% endfor %}

关键本质上是将所需的信息在列表中配对在一起,然后在模板中一起迭代它们以避免 for 循环问题。

由于您没有将模型与模型配对,因此您只需将participantList = Approval.objects.filter(study=study)和所有这些替换为对带有主类别名称作为参数的字典的调用。

为了创建子类别模型...

Category(models.Model):
name = models.Charfield()
Sub_Category(models.Model):
name = models.CharField()
category = models.ManyTOManyField(Sub_Category)
def home(request):
categories = Category.objects.all().order_by('-date_posted')
cid = #Generate your Category id single or list...
//you can ever it multiple time with for loop...
sub_categories = Sub_Category.objects.filter(category=cid) 
context = {
'categories': categories,
'sub_categories': sub_categories
}
return render(request, 'blog/home.html', context)

最新更新