提问者:小点点

我正在使用python,django和PostgreSQL为我的站点创建一个产品页面。我在比较两个表中的字段时遇到了一个问题


目前在数据库中,我有两个表1.category(id,type)和2.products(id,category,name,price)。 我想显示产品只有当“类型的类别”是相同的“类别的产品”。 这就是我的做法,但它显示出错误。

                         {% for i in types %}
                         {% for j in prods %}

                            {% if i.type== j.category %}

                            <div class="product-img">
                                <img src="{{j.disimage.url}}" alt="">
                                
                            </div>
                            <div class="product-caption">
                                
                                <h4><a href="#">{{j.name}}</a></h4>
                                <div class="price">
                                    <ul>
                                        <li>RS {{j.currentprice}}</li>
                                        <li class="discount">RS {{j.originalprice}}</li>
                                    </ul>
                                </div>
                            </div>
                            {% endif %}
                            {% endfor %}
                            {% endfor %}
                        

此行显示错误。{%if I.Type==J.Category%}

这里是models.py。

  from django.db import models
  from datetime import datetime
  from django.utils import timezone

  class Category(models.Model):
      type=models.CharField(max_length=30)

  class Product(models.Model):
      category=models.CharField(max_length=30)

      name=models.CharField(max_length=30)
      currentprice=models.IntegerField()
      disimage=models.ImageField(upload_to='pics')

这是我得到的错误:无法解析“i.Type==”中的余数:“==”

请告诉我如何实现。谢谢


共1个答案

匿名用户

这里需要给出if条件中的空格

{% if i.type == j.category %}

但是

我想如果你们保持你们模型之间的关系会更好。 例如,使用ManytoOne关系(即ForeignKey)将更好地解决这个问题。 查看文档