0

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.

Amarendra Reddy
  • 243
  • 2
  • 17

1 Answers1

0

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 %}
  • The files are uploaded by user so using static is not possible. I found the problem with my settings.py. The MEDIA_ROOT variable was not able to get the absolute path which was causing the problem. I have corrected it and it works fine. – Amarendra Reddy Jun 01 '16 at 06:54