简体   繁体   English

Django图片字段未从存储中检索

[英]Django image field not retrived from storage

I plot a graph and save it as an image in my model. 我绘制一个图形并将其另存为模型中的图像。 But, the file is not getting retrived at html page. 但是,该文件不会在html页面上检索到。 My models.py: 我的models.py:

class MyModel(models.Model):
      ... 
      ...
      photo = models.ImageField(upload_to='graphs',blank=True)

settings.py: settings.py:

MEDIA_ROOT =  os.path.join(BASE_DIR,"media")

MEDIA_URL =  "/media/"

The file is getting stored in the media/graphs directory. 该文件将存储在media / graphs目录中。 {{ MyModel.photo.url }} is printed correctly as /media/graphs/file_name . {{ MyModel.photo.url }}正确打印为/media/graphs/file_name But when put in an image tag, it is not getting displayed. 但是当放入图像标签时,它不会显示。 Why is this happening so? 为什么会这样呢?

MEDIA_ROOT =  os.path.join(BASE_DIR,"/media/")

evals to /media/ , because /media/ is already an absolute path. 等效于/media/ ,因为/media/已经是绝对路径。 You want 你要

MEDIA_ROOT =  os.path.join(BASE_DIR,"media")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM