简体   繁体   中英

Media Url not resolving properly

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.

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM