Django models Django只显示一个元素

Django models Django只显示一个元素,django-models,django-templates,django-views,Django Models,Django Templates,Django Views,我有带图片的产品,但当我尝试只显示图片时,django向我显示所有图片产品如何修复它?我试着用切片不起作用,因为如果我用切片:“0:1”给我看图片,但不要给我看这个产品的图片 class Product(models.Model): category = models.ForeignKey(Category, related_name='product') name = models.CharField(max_length=200, db_index=True) slu

我有带图片的产品,但当我尝试只显示图片时,django向我显示所有图片产品如何修复它?我试着用切片不起作用,因为如果我用切片:“0:1”给我看图片,但不要给我看这个产品的图片

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])
我的观点

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})
还有my 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 %}
{%extensed“shop/base.html”%}
{%load static%}
{%block title%}{{product.name}{%endblock%}
{%block content%}
{产品%中的p的百分比}
{%endfor%}
{%endblock%}
如果你想要更多的评论,我会尽快添加。

改变你的观点

def product_detail(request, id, slug):
    product = get_object_or_404(Product,
                                id=id,
                                slug=slug,
                                available=True)

    return render(request,
                  'shop/product/detail.html',
                  {'product': product})
和模板更改

{% for p in products %}

您可以通过删除模板中的一个来解决这个问题,但我建议更改前面提到的视图功能

如果您使用枕头,而不仅仅是图像的地址作为字符串,请更改此设置

{{p.detail_url}}


您只想显示一种产品?因为您正在循环所有产品。你从其他地方复制了这个代码吗?没有,是我创建的。我做完了。Mby很容易,但我不知道如何学好它很难理解你的英语,你的问题是什么,你的母语是什么?也许里面有stackoverflow的版本
{{p.detail_url}}
{{p.fieldname.url}}