【问题标题】:Trying to display multiple image for my product尝试为我的产品显示多个图像
【发布时间】:2020-07-10 00:51:36
【问题描述】:

我正在尝试为一种产品显示多张图片,但图片无法显示,我不知道为什么有人可以提供帮助。

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

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/'

【问题讨论】:

  • 尝试删除 MEDIA_URL, src="{{ img.image.url }}" 浏览器控制台是否出现任何错误?
  • @danish_wani 不,我没有
  • 删除 MEDIA_URL
  • @danish_wani 不,我已经尝试过了,但没有成功
  • 检查html,看看图片的url是什么

标签: django django-templates django-media


【解决方案1】:
{% 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 %}

【讨论】:

  • 仍然不显示
  • @Eman... 你确定你在 settings.py 和 url 中有静态 url 吗?
  • @Eman.. 好的,你有图片保存在数据库中吗?因为如果不保存就不会显示。
  • @Eman...这应该可以,我可以看到您正在过滤产品的 ID。为什么不直接将图像移动到 Product 模型中,这样可以很容易地调用视图,而不是为此创建单独的模型。
  • 我正在尝试为一个产品显示多个图像
【解决方案2】:

从源中移除 {{MEDIA_URL}}。

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

【讨论】:

    猜你喜欢
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2021-12-11
    • 2020-01-26
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多