尝试为我的产品显示多个图像



我试图为一个产品显示多个图像,但这些图像无法显示,我不知道为什么有人能提供帮助。

我的模型。我必须图像字段一个用于显示和产品,如果用户想要更多细节,另一个用于展示产品的更多图像。

class Product(models.Model):
name = models.TextField()
slug = models.SlugField(null=True, unique=True)
description = models.TextField()
stock = models.SmallIntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)

def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('single',args=[self.id,self.slug])
class ProductImage(models.Model):
product = models.ForeignKey('Product', on_delete=models.CASCADE)
image = models.ImageField(upload_to='images/', blank=True)
featured = models.BooleanField(default=False)
thumbnail = models.BooleanField(default=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
active = models.BooleanField(default=True)
def __unicode__(self):
return self.product.image

视图

def single(request, id, slug):
try:
# products = Product.objects.get(slug=slug)
product = get_object_or_404(Product,
id=id,
slug=slug)
images = ProductImage.objects.filter(product=product)
return render(request, "single.html",
{'product': product},
{'images':images})
except:
raise Http404

我是如何尝试在Html页面上显示图像的。

{% for img in images %}
{% if item.featured %}
<img class="img-responsive"src="{{ MEDIA_URL }}{{ img.image.url }}" height="px" width="px" class="pr-5 mr-5">
{% endif %}
{% if not item.featured %}
<img class="img-responsive"src="{{ MEDIA_URL }}{{ img.image.url }}" height="px" width="px" class="pr-5 mr-5">
{% endif %} 
{% endfor %}

设置

STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'),
)
CRISPY_TEMPLATE_PACK = 'bootstrap3'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
LOGIN_REDIRECT_URL = '/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
LOGIN_URL = '/account/login/'
{% for img in images %}
{% if img.image %}
<img class="img-responsive"src="{{ img.image.url }}" height="px" width="px" class="pr-5 mr-5">
{% endif %}
{% endfor %}

从src中删除{{MEDIA_URL}}。

src="{{img.image.url}}">

最新更新