0
votes

I've this hiearchy:

-mega_series
   -mega_series
   -series
      -models.py
      -views.py
      ...
   -manage.py
   -media
      -covers
   -static

The MEDIA_URL AND MEDIA_ROOT are the following:

MEDIA_ROOT = '/Users/lechucico/Documents/mega_series/media/'
MEDIA_URL = 'media/' 

On the model I store the image like that:

class Serie (models.Model):
   serie_cover = models.ImageField(upload_to='covers')
   serie_name = models.CharField(max_length=100)

The images once uploaded via admin site goes correctly to path: mega_series/media/covers/

And this is how I acces it via html:

<img src="{{serie.serie_cover.url}}" width="150" height="250" />

The result url that I got is the following:

http://127.0.0.1:8000/media/covers/juego_de_tronos.jpg

Why the image doesn't appear on the html?

Thanks.

1
what is your MEDIA_ROOT and MEDIA_URL in settings.pyNiyojan
try using serie.serie_cover.urlNiyojan
@Niyojan - Might want to put that as the answer, as it's the most likely causeJens Astrup
@Niyojan I don't have MEDIA_ROOT and MEDIA_URL. serie.serie_cover.url produces the same result.Lechucico

1 Answers

1
votes

You would need to define MEDIA_ROOT as mentioned here

From Django officaal Docs:

urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

You can also have a look at this.