0
我有产品的图像,但是当我尝试显示图像只有这个产品django显示我所有图像产品如何我可以修复它?我尝试切片不工作,如果我切片:“0:1”显示图像,但不是图像这个产品。Django只显示一个元素
class Product(models.Model):
category = models.ForeignKey(Category, related_name='product')
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
image = models.ImageField(upload_to='product/%Y/%m/%d',
blank=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
promo = models.BooleanField(default=False)
news = models.BooleanField(default=True)
section = models.CharField(max_length=50, choices=SECTION_CHOICES, default=None)
detailimage = models.ImageField(upload_to='product/detail/%Y/%m/%d',
blank=True)
detailimagetwo = models.ImageField(upload_to='product/detail/%Y/%m/%d',
blank=True)
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
@property
def image_url(self):
if self.image and hasattr(self.image, 'url'):
return self.image.url
@property
def detail_url(self):
if self.detailimage and hasattr(self.detailimage, 'url'):
return self.detailimage.url
@property
def detailtwo_url(self):
if self.detailimagetwo and hasattr(self.detailimagetwo, 'url'):
return self.detailimagetwo.url
def get_absolute_url(self):
return reverse('shop:product_detail',
args=[self.id, self.slug])
我views.py
def product_detail(request, id, slug):
product = get_object_or_404(Product,
id=id,
slug=slug,
available=True)
products = Product.objects.all()
return render(request,
'shop/product/detail.html',
{'product': product,
'products': products})
和我detail.html
{% extends "shop/base.html" %}
{% load static %}
{% block title %}{{ product.name }}{% endblock %}
{% block content %}
{% for p in products %}
<img src="{{p.detail_url}}" class="img-responsive">
{% endfor %}
</div>
</div>
{% endblock %}
如果u想somethink更多的评论,我会多快,我可以添加它。
你想只显示一个产品的使用枕头的图像作为字符串的不仅仅是地址发生变化?因为你在循环所有产品。你是否从别的地方复制这段代码? – Mojimi
不,我创建它。我完成了。 Mby很简单,但我不知道该怎么做 – KeepItSick
那么它很难理解你的英语,你的问题是什么,你的母语是什么?也许有它的版本的计算器 – Mojimi