如何从查询集值中在Django模板语言中访问外键?

huangapple go评论67阅读模式
英文:

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=&quot;maincategory&quot;)
    name = models.CharField(max_length=100)

    def __str__(self) :
        return self.name + &quot;--&quot; + self.main_category.name
    
class SubCategory(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name=&quot;category_name&quot;)
    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 = {
        &#39;sliders&#39; : sliders,
        &#39;banners&#39; : banners,
        &#39;main_category&#39; : main_category
    }
    return render(request, &#39;main/home.html&#39;, 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

&lt;div class=&quot;cat__menu&quot;&gt;
                                &lt;nav id=&quot;mobile-menu&quot; style=&quot;display: block;&quot;&gt;
                                    &lt;ul&gt;

                                        {% for i in main_category %}

                                        &lt;li&gt;
                                            &lt;a href=&quot;shop.html&quot;&gt; {{ i.name }} &lt;i class=&quot;far fa-angle-down&quot;&gt;&lt;/i&gt;&lt;/a&gt;
                                            &lt;ul class=&quot;mega-menu&quot;&gt;

                                                {% for cat in i.category_set.all %}

                                                &lt;li&gt;&lt;a href=&quot;shop.html&quot;&gt;{{cat.name}}&lt;/a&gt;
                                                    &lt;ul class=&quot;mega-item&quot;&gt;
                                                        {% for sub_cat in cat.subcategory_set.all %}
                                                        &lt;li&gt;&lt;a href=&quot;product-details.html&quot;&gt;{{sub_cat.name}}&lt;/a&gt;&lt;/li&gt;
                                                        {% endfor %}
                                                        
                                                    &lt;/ul&gt;
                                                &lt;/li&gt;
                                                {% endfor %}
  
                                            &lt;/ul&gt;
                                        &lt;/li&gt;

                                        {% endfor %}
                                        
                                    &lt;/ul&gt;
                                &lt;/nav&gt;
                            &lt;/div&gt;
  

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,所以它不起作用。因此,您应该使用该相关名称,而不是&lt;modelname&gt;_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 &lt;modelname&gt;_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 %}

huangapple
  • 本文由 发表于 2023年5月11日 15:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定