Django:从管理中的静态文件夹渲染图像

时间:2019-01-29 16:12:40

标签: django

我需要为我在Admin中上传的所有文件渲染缩略图。

我正在使用Adobe Illustrator的徽标来表明这些文件确实是AI文件。

但是,它没有被渲染。在HTML中,我得到:

<img src="static/img/admin/adobe_illustrator_file_logo.png" width="200px" height="180px">

结果如下:

enter image description here

为什么?

class OrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    product = models.CharField(max_length= 200)
    quantity = models.CharField(max_length= 200)
    size = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name= 'PEN Price')
    file = models.FileField(upload_to='files', blank=True, null=True)
    comment = models.CharField(max_length=200, blank=True, null=True, default='')
    uploaded_at = models.DateTimeField(auto_now_add=True)



    class Meta:
        db_table = "OrderItem"

    def file_thumbnail(self):
        if self.file:
           return mark_safe(u'<img src="%s" width="200px" height="180px" />' % ('static/img/admin/adobe_illustrator_file_logo.png'))
        else:
            pass

1 个答案:

答案 0 :(得分:2)

这是相对路径:

nest

请注意,开头没有任何 static/img/admin/...

此路径等于以下内容:

/

解决方案:

因此,您需要使用绝对路径(以 http://example.com/current/page/url/static/img/admin/... 开头):

/

此问题将得到适当解决:

/static/img/admin/...

有关MDN docs的更多信息。