Django-图片未显示在网页404上

时间:2019-03-18 09:37:14

标签: django python-3.x

个人资料页面未显示通过models.py中的upload_to函数上传的个人资料图片

option/s

ImageField-> def upload_to(instance, filename): return 'users/%s/%s' % (instance.user, filename)

用于user_profile的

url是

img = models.ImageField(upload_to=upload_to, blank=True, db_index=True)

user_profile.html的html页面是这样的

url(r'^users/(?P<username>[\w-]+)/$', views.user_profile, name='profile'),

views.py ---->

<li>
    I am The Image
    <img src="{{ image }}" alt="img">
</li>

服务器控制台中显示的网址是

  

/users/rony/f4098984_Kgo0lqi.jpg HTTP / 1.1“ 404 2665

在上述的jpg图像链接上单击上方的代码时,该图像在上述链接处可用,但该图像仍未显示在网页中

下面提供了github问题链接。 https://github.com/bikirandas/OmexOnline/issues/2

1 个答案:

答案 0 :(得分:0)

我已找到我发布的上述问题的解决方案。

按如下所示更改html代码。

<img src="{{ MEDIA_ROOT }}{{ image.url}}" alt="Profile Picture">

views.py 如下所示

def user_profile(request, username):
    image = request.user.profile.img
    # image = str(image)
    # image = image.strip('users/%s' % username)
    return render(request, 'user_profile.html', {'username': request.user.username,
                                                 'image': image})

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)添加到 urls.py

相关问题