初学者问题。我如何显示产品的所有图像?姜戈 + Python
Beginner problem. How i can display all images from a product? Django + Python
我是初学者,需要一些帮助。
我想显示一个产品的所有图片,但找不到正确的方法。
代码如下:
models.py:
class Product(models.Model):
name = models.CharField(max_length=200)
short_description = models.CharField(
max_length=200, null=True, blank=True, help_text="Enter a brief description of the product")
description = models.TextField(blank=True, null=True)
price = models.DecimalField(max_digits=7, decimal_places=2)
digital = models.BooleanField(default=False, null=True, blank=True)
default_image = models.ImageField(null=True, blank=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('product-detail', args=[str(self.id)])
@property
def imageURL(self):
try:
url = self.default_image.url
except:
url = ''
return url
def image_tag(self):
return mark_safe('<img src="{}" height="50"/>'.format(self.default_image.url))
image_tag.short_description = 'Image'
class ProductImage(models.Model):
product = models.ForeignKey(
Product, on_delete=models.CASCADE, default=None, related_name='images')
title = models.CharField(max_length=50, null=True, blank=True)
image = models.ImageField(null=True, blank=True)
def __str__(self):
return self.product.name
这是观点:
class ProductDetail(DetailView):
model = Product
URL:
path('product/<int:pk>', views.ProductDetail.as_view(), name='product-detail'),
和模板product_detail.html:
{% extends 'store/home.html' %}
{% load static %}
{% block content %}
<div>
<h1>{{ product.name }}</h1>
<p>{{ product.description }}</p>
<!--<img src="{{product.imageURL}}" alt=""> -->
</div>
{% endblock %}
评论仅返回默认图像。
缩进在我的 VS 代码中是正确的,但这里很难写。
任何建议将不胜感激。
谢谢。
尝试:
{% for product_image in product.images.all %}
<img src="{{ product_image.image.url }}" alt="{{ product_image.title }}">
{% endfor %}
代替 commented-out <img src="{{product.imageURL}}" alt="">
。这里的关键是product.images.all
的使用。我建议您阅读 Django ORM 的 reverse relations feature.
我是初学者,需要一些帮助。 我想显示一个产品的所有图片,但找不到正确的方法。
代码如下:
models.py:
class Product(models.Model):
name = models.CharField(max_length=200)
short_description = models.CharField(
max_length=200, null=True, blank=True, help_text="Enter a brief description of the product")
description = models.TextField(blank=True, null=True)
price = models.DecimalField(max_digits=7, decimal_places=2)
digital = models.BooleanField(default=False, null=True, blank=True)
default_image = models.ImageField(null=True, blank=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('product-detail', args=[str(self.id)])
@property
def imageURL(self):
try:
url = self.default_image.url
except:
url = ''
return url
def image_tag(self):
return mark_safe('<img src="{}" height="50"/>'.format(self.default_image.url))
image_tag.short_description = 'Image'
class ProductImage(models.Model):
product = models.ForeignKey(
Product, on_delete=models.CASCADE, default=None, related_name='images')
title = models.CharField(max_length=50, null=True, blank=True)
image = models.ImageField(null=True, blank=True)
def __str__(self):
return self.product.name
这是观点:
class ProductDetail(DetailView):
model = Product
URL:
path('product/<int:pk>', views.ProductDetail.as_view(), name='product-detail'),
和模板product_detail.html:
{% extends 'store/home.html' %}
{% load static %}
{% block content %}
<div>
<h1>{{ product.name }}</h1>
<p>{{ product.description }}</p>
<!--<img src="{{product.imageURL}}" alt=""> -->
</div>
{% endblock %}
评论仅返回默认图像。 缩进在我的 VS 代码中是正确的,但这里很难写。 任何建议将不胜感激。 谢谢。
尝试:
{% for product_image in product.images.all %}
<img src="{{ product_image.image.url }}" alt="{{ product_image.title }}">
{% endfor %}
代替 commented-out <img src="{{product.imageURL}}" alt="">
。这里的关键是product.images.all
的使用。我建议您阅读 Django ORM 的 reverse relations feature.