I plot a graph and save it as an image in my model. But, the file is not getting retrived at html page. My models.py:
class MyModel(models.Model):
...
...
photo = models.ImageField(upload_to='graphs',blank=True)
settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR,"media")
MEDIA_URL = "/media/"
The file is getting stored in the media/graphs directory. {{ MyModel.photo.url }}
is printed correctly as /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. You want
MEDIA_ROOT = os.path.join(BASE_DIR,"media")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.