显示来自数据库Digital Ocean的图像

时间:2018-11-10 21:52:39

标签: python django sqlite

为什么图像没有显示在模板中? 在本地,一切都很好,但是当我将其在线推送时(在浏览器中)似乎不存在图像(即使我可以看到它们已正确保存在models.py指向的位置)。

我正在使用digitalocean一键式功能。

DB是SQLite。

这是我的代码:

settings.py

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

STATIC_URL = '/static/'

models.py

class ScreenShots(models.Model):
    img = models.ImageField(upload_to='img_api/static/img_api')
    ...

    @property
    def filename(self):
        return os.path.basename(self.img.name)

    def get_img_url(self):
        return '/static/img_api/' + self.filename

模板:

 <img src="{{ img.get_img_url }}">
 <img src:"/static/img_api/test.jpg"> - that's how it looks in browser

文件夹结构:

|- project
|   |- setting.py etc.
|- img_api
|    |- models.py
|    | - ...
|    |- static
|        |- img_api
|            |- here .jpg files are saved here

0 个答案:

没有答案
相关问题