首页 > 解决方案 > 无法从 Django 管理员显示图像

问题描述

我正在尝试查看/显示我在产品模型中上传的图像。产品 html 以查看产品中的内容

这是产品型号代码

这就是我运行代码查看产品时得到的结果

下面是views.py 代码:

def ProductDetailtListView(request, pk=None, *args, **kwargs):
#instance = Product.objects.get(pk=pk)#id
instance = get_object_or_404(Product, pk=pk)
context = {


    'object': instance
}

return render(request, 'MainApp/productDetail.html', context)

下面是 models.py 代码:

class Product(models.Model):

title = models.CharField(max_length=120)
description = models.TextField()
price = models.DecimalField(decimal_places=2, max_digits=10, default=39.99)
image = models.FileField(upload_to=upload_image_path, null= True, blank=True)

def __str__(self):
    return self.title


def __unicode__(self):
    return self.title

这是 productDetailListView.html 页面:

{% load static %}
{{ object.title }} <br/>
{{ object.description }} <br/>
{{ object.image.url }}
<img src="{{ object.image.urls }}" class='img-fluid'/>

标签: djangodjango-models

解决方案


推荐阅读