[Django]-Django : Display uploaded image with media

2👍

Correct configuration for media files in settings.py

# Media files, uploaded by user
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Template

<a href="{{item.cover.url}}" target="_blank">
    <!-- An alt tag in span makes no sense, so you can use title tag if you want. -->
    <span class="glyphicon glyphicon-home" title="{{item}}"></span>
    <!-- OR show item's str inside span -->
    <span class="glyphicon glyphicon-home">{{item}}</span>
</a>

2👍

you have add the upload_to=’media/’, in the image fields so it will create one more folder inside your media root. Django already media in image path. complete path become
mediaurl+folderpath

cover = models.ImageField(upload_to='other_folder_name', verbose_name=_('cover page'))

and

MEDIA_URL = '/media/'

because it take relative path, without start with /

<a href="{{item.cover.url}}" target="_blank">
        <span class="glyphicon glyphicon-home" alt="{{item.cover}}"></span>
</a>

Leave a comment