是否将与产品相关的图像从对象列表显示到Django模板?

2024-10-03 06:30:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里,我试图在弹出窗口中显示与特定产品相关的所有图像。在这里,我通过templateproductmapping表从template表获取所有产品模板图像列表

型号.py

# this is the product table
class Product(models.Model):
    prod_ID = models.AutoField("Product ID", primary_key=True)
    prod_Name = models.CharField("Product Name", max_length=30, null=False)
    prod_Desc = models.CharField("Product Description", max_length=2000, null=False)
    prod_Price = models.IntegerField("Product Price/Piece", default=0.00)
    prod_img = models.ImageField("Product Image", upload_to='productImage/', null=True)

# this is the imageTemplate table where I am storing template images 
class ImageTemplate(models.Model):
    temp_id = models.AutoField("Template ID", primary_key=True, auto_created=True)
    temp_img = models.ImageField("Template Image",upload_to='Template Images/', null=False)

# here in this table I am storing mappings of images to a product, so I can identify which image belongs to which product so I can use all the images related to a single product.
class ImageTemplateProductMapping(models.Model):
    imageTemp_p_map_id = models.AutoField("Template Image & Product Map ID", primary_key=True, auto_created=True)
    template_img_id = models.ForeignKey(ImageTemplate, null=False, on_delete=models.CASCADE,
                                        verbose_name="Image Template ID")
    prod_id = models.ForeignKey(Product, null=False, on_delete=models.CASCADE, verbose_name="Product Id")

视图.py

def product(request, id):
    products = Product.objects.get(prod_ID=id)
    ImageTemplateProductslist = []
    try:
        ImageTemplateProductsmap = ImageTemplateProductMapping.objects.filter(prod_id=id)
        #here i am getting all the images related to a single product.
        ImageTemplateProductslist = [data.temp_id.temp_img for data in
                                     ImageTemplateProductsmap]
    except AttributeError:
        pass
    context = {"ImageTemplateProductslist": ImageTemplateProductslist}
    return render(request, "GalaxyOffset/product.html", context)

product.html

<div class="col-5 mr-4 mt-2">
    {% for s in ImageTemplateProductslist %}
    <!--Here in this image tag I want to show that images-->
    <img class="border border-secondary rounded my-2 mx-3" height="100"
                                                 src="{{s.url}}"
                                                 width="100"/>
    <br>
    {% endfor %}
</div>

你能帮我解决这个问题吗


Tags: toidfalsetrueimgmodelstemplateprod
1条回答
网友
1楼 · 发布于 2024-10-03 06:30:06

在我没有注意到的地方,我犯了一个非常愚蠢的错误,在ImageTemplate表中我使用temp_id作为主键,在ImageTemplateProductMapping表中,我将template_img_id作为外键传递,然后使用temp_id作为引用从ImageTemplate获取数据。这就是代码不适合我的原因。让我再次共享更改后的代码:

型号.py

# this is the product table
class Product(models.Model):
    prod_ID = models.AutoField("Product ID", primary_key=True)
    prod_Name = models.CharField("Product Name", max_length=30, null=False)
    prod_Desc = models.CharField("Product Description", max_length=2000, null=False)
    prod_Price = models.IntegerField("Product Price/Piece", default=0.00)
    prod_img = models.ImageField("Product Image", upload_to='productImage/', null=True)

# this is the imageTemplate table where I am storing template images
class ImageTemplate(models.Model):
    temp_id = models.AutoField("Template ID", primary_key=True, auto_created=True)
    temp_img = models.ImageField("Template Image",upload_to='Template Images/', null=False)

# here in this table I am storing mappings of images to a product, so I can identify which image belongs to which product so I can use all the images related to a single product.
class ImageTemplateProductMapping(models.Model):
    imageTemp_p_map_id = models.AutoField("Template Image & Product Map ID", primary_key=True, auto_created=True)
    temp_id = models.ForeignKey(ImageTemplate, null=False, on_delete=models.CASCADE,
                                        verbose_name="Image Template ID")
    prod_id = models.ForeignKey(Product, null=False, on_delete=models.CASCADE, verbose_name="Product Id")

视图.py

def product(request, id):
    products = Product.objects.get(prod_ID=id)
    ImageTemplateProductslist = []
    try:
        ImageTemplateProductsmap = ImageTemplateProductMapping.objects.filter(prod_id=id)
        #here i am getting all the images related to a single product.
        ImageTemplateProductslist = [data.temp_id.temp_img for data in
                                     ImageTemplateProductsmap]
    except AttributeError:
        pass
    context = {"ImageTemplateProductslist": ImageTemplateProductslist}
    return render(request, "GalaxyOffset/product.html", context)

product.html

<div class="col-5 mr-4 mt-2">
    {% for s in ImageTemplateProductslist %}
    <! Here in this image tag I want to show that images >
    <img class="border border-secondary rounded my-2 mx-3" height="100"
                                                 src="{{s.url}}"
                                                 width="100"/>
    <br>
    {% endfor %}
</div>

相关问题 更多 >