简体   繁体   中英

Django doesn't put /media/ in front of image url outside of admin section

In the admin area of Django, the image is well served because django automatically puts '/media/' in front of the image url.

But in the public part of my website, when I do this:

 <img class="card-img-top" src="{{ article.image_desc }}" alt="Card image cap">

The image is not. served because the url is not complete.

How to easily fix this issue? I could do this:

 <img class="card-img-top" src="/media/{{ article.image_desc }}" alt="Card image cap">

But it's not convenient at all.

By using {{ article.image_desc }} Django is not getting the full path of the image.

To fix that you should just do the following:

{{ article.image_desc.url }}

Also, you need to have the following in the settings.py :

MEDIA_ROOT = os.path.join(BASE_DIR, 'data/') # 'data' is my media folder
MEDIA_URL = '/media/'

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