【发布时间】:2021-03-27 19:08:27
【问题描述】:
在这里,我试图在弹出窗口中显示与特定产品相关的所有图像。这里我通过templateproductmapping表从模板表中获取所有产品模板图片列表。
models.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")
views.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>
你能帮忙解决这个问题吗?
【问题讨论】:
-
那有什么问题?
-
在产品模板上我有一个弹出窗口,我想在该弹出窗口中显示与该产品相关的所有图像以供选择,但我不知道如何在那里获取这些图像,我尝试了不同的选项,但没有一个有效。你能帮我弄清楚我做错了什么,我应该怎么做?
标签: python django database django-views django-templates