正如您在 SS 中看到的,我无法在管理面板中查看图像;在图片标签下
来自 CMD 的错误:
Not Found: /upload/images/How-to-buy-a-desktop-PC-header.jpg
[27/Jun/2021 13:00:51] "GET /upload/images/How-to-buy-a-desktop-PC-header.jpg HTTP/1.1" 404 2605
我已经检查了上传文件夹,图片在文件夹中。
settings.py
MEDIA_URL = '/upload/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'upload')
urls.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
models.py
# under the Product class
def image_tag(self):
return mark_safe('<img src="{}" height ="50"/>'.format(self.image.url))
image_tag.short_discription = 'Image'
最后一个 admin.py
class ProductAdmin(admin.ModelAdmin):
list_display = ['title', 'category', 'status', 'image_tag']
list_filter =['category']
readonly_fields = ('image_tag',)
inline = [ProductImageInline]