Django image field not retrived from storage

175 views Asked by At

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?

1

There are 1 answers

2
bruno desthuilliers On
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")