当前位置: 动力学知识库 > 问答 > 编程问答 >

django - Unable to display uploaded image file

问题描述:

I am currently using this structure to upload images on my site:

I am testing on my localhost:8000

In my setting.py:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')

MEDIA_URL = '/media/'

In my Photo model I defined the following:

image = models.ImageField(upload_to='site_images')

The image uploads correctly but whenever I do the following:

{% for photo in photos %}

<div class="col-md-3 photo-wrapper">

<img src="{{ photo.image.url }}"/>

</div>

{% endfor %}

In the page source it is looking for the following URL where it is unable to retrieve the photo from:

http://media/profile_photos/photo_name.jpeg

How would one go about fixing this?

网友答案:

The Problem was the MEDIA_URL = '/media/' that should be MEDIA_URL = 'media/', someone answered this but I see he removed his answer. This fixed my problem and images are now shown.

分享给朋友:
您可能感兴趣的文章:
随机阅读: