Media Url not resolving properly

231 Views Asked by At

I have the media url and media root as follows.

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, MEDIA_URL)

my urls.py is

if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I am accessing it in the template as follows:

<img class="hl" src="{{ MEDIA_URL }}prop/image0.png" /></a>

The url replaced when rendered is correct, which is /media/prop/image0.png.

But it says the media location is not found.

1

There are 1 best solutions below

1
Danial Noori On

I suggest you use staticfile for this purpose, because you are using a specific image and not something that will be uploaded in the future.

If you decide to use static files, put the image in your static folder and then use this:

 <img src="{% static 'image0.png' %}" style="">

Don't forget to load your staticfiles:

{% load staticfiles %}

By the way, for future, if you wanted to use media files in your templates you have to do it like this:

{% for image in images %}
    <img src="{{ image.url }}" class="h1">
{% endfor %}