英文:
How to acess Foreign key on django template languages from queryset values
问题
这是您的代码部分的翻译:
class MainCategory(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Category(models.Model):
main_category = models.ForeignKey(MainCategory, on_delete=models.CASCADE, related_name="maincategory")
name = models.CharField(max_length=100)
def __str__(self):
return self.name + "--" + self.main_category.name
class SubCategory(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="category_name")
name = models.CharField(max_length=100)
def __str__(self):
return self.name
视图部分的翻译:
def Home(request):
sliders = Slider.objects.all()
banners = BannerArea.objects.all()
main_category = MainCategory.objects.all()
context = {
'sliders': sliders,
'banners': banners,
'main_category': main_category
}
return render(request, 'main/home.html', context)
头部 HTML 文件的翻译:
<div class="cat__menu">
<nav id="mobile-menu" style="display: block;">
<ul>
{% for i in main_category %}
<li>
<a href="shop.html"> {{ i.name }} <i class="far fa-angle-down"></i></a>
<ul class="mega-menu">
{% for cat in i.category_set.all %}
<li><a href="shop.html">{{cat.name}}</a>
<ul class="mega-item">
{% for sub_cat in cat.subcategory_set.all %}
<li><a href="product-details.html">{{sub_cat.name}}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</nav>
</div>
您希望显示主类别(Main Category)、子类别(Sub Category)和类别(Category)。如果子类别和类别没有显示,请检查模型和视图的逻辑,确保数据正确加载和传递到模板中。如果问题仍然存在,您可能需要检查模型关系和数据库中的数据是否正确设置。
英文:
Here is my model
class MainCategory(models.Model):
name = models.CharField(max_length=100)
def __str__(self) :
return self.name
class Category(models.Model):
main_category = models.ForeignKey(MainCategory, on_delete=models.CASCADE, related_name="maincategory")
name = models.CharField(max_length=100)
def __str__(self) :
return self.name + "--" + self.main_category.name
class SubCategory(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="category_name")
name = models.CharField(max_length=100)
def __str__(self) :
return self.name
View
def Home(request):
sliders = Slider.objects.all()
banners = BannerArea.objects.all()
main_category = MainCategory.objects.all()
context = {
'sliders' : sliders,
'banners' : banners,
'main_category' : main_category
}
return render(request, 'main/home.html', context)
I want to display Main Category and Sub category and category. Main Category is displayed but sub category and category are not displayed. Can you help me to fix this problem
Header.html files
<div class="cat__menu">
<nav id="mobile-menu" style="display: block;">
<ul>
{% for i in main_category %}
<li>
<a href="shop.html"> {{ i.name }} <i class="far fa-angle-down"></i></a>
<ul class="mega-menu">
{% for cat in i.category_set.all %}
<li><a href="shop.html">{{cat.name}}</a>
<ul class="mega-item">
{% for sub_cat in cat.subcategory_set.all %}
<li><a href="product-details.html">{{sub_cat.name}}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</nav>
</div>
I want to display Main Category and Sub category and category. Main Category is displayed but sub category and category are not displayed. Can you help me to fix this problem
答案1
得分: 0
因为您为反向关系定义了related name,所以它不起作用。因此,您应该使用该相关名称,而不是<modelname>_set
,像这样:
{% for i in main_category %}
{% for cat in i.maincategory.all %}
{{ cat }}
{% for sub_cat in cat.subcategory_set.all %}
{{ sub }}
{% endfor %}
{% endfor %}
{% endfor %}
英文:
It is not working because you have defined related name for reverse relationship. Hence instead of <modelname>_set
you should use that related name, like this:
{% for i in main_category %}
{% for cat in i.maincategory.all %}
{{ cat }}
{% for sub_cat in cat.subcategory_set.all %}
{{ sub }}
{% endfor %}
{% endfor %}
{% endfor %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论