使用外键比较两个模型的字段。(用于在正确的类别下显示正确的产品)使用 django
Comparing fields from two models using foreign key.(for displaying correct product under correct category) using django
我有两个模型类别和产品。这里是models.py
class Category(models.Model):
type=models.CharField(max_length=30)
def __str__(self):
return self.type
class Product(models.Model):
category = models.ForeignKey(Category, on_delete = models.CASCADE)
productid=models.CharField(max_length=30)
name=models.CharField(max_length=30)
disimage=models.ImageField(upload_to='pics')
def __str__(self):
return self.productid
在我的模板中,我有两个 for 循环,一个迭代类别模型,另一个迭代产品,我只想在 category.type 等于产品类别时显示产品(对于例如,如果类别中有一个 type=Shirt 那么我只想在它下面显示一个产品,如果它有 type=Shirt )
这是我的HTML
{% for i in types %}
<div class="container">
<div class="row product-btn d-flex justify-content-end align-items-end">
<!-- Section Tittle -->
<div class="col-xl-4 col-lg-5 col-md-5">
<div class="section-tittle mb-30" id="{{i.type}}">
<h2>{{i.type}}</h2>
</div>
</div>
</div>
<div class="tab-content" id="nav-tabContent">
<!-- card one -->
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<div class="row">
{% for product in prods %}
{% if i.type == product.category %}
<div class="col-xl-4 col-lg-4 col-md-6">
<div class="single-product mb-60">
<div class="product-img">
<img src="{{product.disimage.url}}" alt="">
</div>
<div class="product-caption">
<h4><a href="#" id="{{product.productid}}">{{product.name}}</a></h4>
<div class="price">
<ul>
<li>RS {{product.currentprice}}</li>
<li class="discount">RS {{product.originalprice}}</li>
<div>
<a href="/category/singleproduct/{{product.productid}}" class="btn header-btn btn-outline-info">View</a>
<a href="#" class="btn header-btn btn-outline-info">Add to Cart</a>
</div>
</ul>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endfor %}
我正在尝试使用 {% if i.type == product.category %} 来做到这一点,但我没有成功。任何有关如何解决此问题的帮助将不胜感激。
你应该使用以下内容
{% if i.type == product.category.type %}
我有两个模型类别和产品。这里是models.py
class Category(models.Model):
type=models.CharField(max_length=30)
def __str__(self):
return self.type
class Product(models.Model):
category = models.ForeignKey(Category, on_delete = models.CASCADE)
productid=models.CharField(max_length=30)
name=models.CharField(max_length=30)
disimage=models.ImageField(upload_to='pics')
def __str__(self):
return self.productid
在我的模板中,我有两个 for 循环,一个迭代类别模型,另一个迭代产品,我只想在 category.type 等于产品类别时显示产品(对于例如,如果类别中有一个 type=Shirt 那么我只想在它下面显示一个产品,如果它有 type=Shirt )
这是我的HTML
{% for i in types %}
<div class="container">
<div class="row product-btn d-flex justify-content-end align-items-end">
<!-- Section Tittle -->
<div class="col-xl-4 col-lg-5 col-md-5">
<div class="section-tittle mb-30" id="{{i.type}}">
<h2>{{i.type}}</h2>
</div>
</div>
</div>
<div class="tab-content" id="nav-tabContent">
<!-- card one -->
<div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">
<div class="row">
{% for product in prods %}
{% if i.type == product.category %}
<div class="col-xl-4 col-lg-4 col-md-6">
<div class="single-product mb-60">
<div class="product-img">
<img src="{{product.disimage.url}}" alt="">
</div>
<div class="product-caption">
<h4><a href="#" id="{{product.productid}}">{{product.name}}</a></h4>
<div class="price">
<ul>
<li>RS {{product.currentprice}}</li>
<li class="discount">RS {{product.originalprice}}</li>
<div>
<a href="/category/singleproduct/{{product.productid}}" class="btn header-btn btn-outline-info">View</a>
<a href="#" class="btn header-btn btn-outline-info">Add to Cart</a>
</div>
</ul>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endfor %}
我正在尝试使用 {% if i.type == product.category %} 来做到这一点,但我没有成功。任何有关如何解决此问题的帮助将不胜感激。
你应该使用以下内容
{% if i.type == product.category.type %}