[Fixed]-MEDIA_URL images not showing on template

1đź‘Ť

âś…

You have a double media in your HTML because upload_to appends to the MEDIA_ROOT / MEDIA_URL. Since you have “media/images” in the upload_to this makes it appear twice. If you want to get rid of it, just make upload_to as “images” and don’t forget to move the directory if you want to see already uploaded images.

Second being that your template has a slight issue:

<img src="/{{ MEDIA_URL }}{{ myobject_instance.image }}">

should be:

<img src="{{ MEDIA_URL }}{{ myobject_instance.image }}">

The initial / will makes it an url that Django won’t find. You’ll get “//media/media/images/8734HDFJ93.jpeg” instead of “/media/media/images/8734HDFJ93.jpeg”

👤Linovia

0đź‘Ť

If you do inspect element in your rendered page, you will see the URL which is generated for the image. Instead of using the variable of “MEDIA_URL”, you can use this template function:

{% get_media_prefix %}

So, finally it would be something like this:

<img src="{% get_media_prefix %}{{ myobject_instance.image }}">

Don’t forget to include this line at the beginning of your template file:

{% load static %}
👤Saeed Farzian

Leave a comment