如何使用Django正确显示用户生成的图像?

2024-06-13 10:05:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图上传一个用户生成的图像,然后显示在我的django网络应用。图像正在上载到服务器,但我无法显示它。你知道吗

你知道吗型号.py你知道吗

image = models.ImageField(blank=True, null=True, max_length=255, upload_to="images/")

你知道吗设置.py你知道吗

MEDIA_ROOT = '/home/user/webapps/static/'
MEDIA_URL = 'http://user.webfactional.com/static/'

例如,假设我上传了一个名为芬兰.gif. 我可以看到上传的文件。然而,当我看图片的来源时,我看到图片的来源是www.foo.com/accounts/profile/images/Finland.gif“而不是应该是的静态图像url”http://user.web派系.com/static/images/Finland.gif". 我该怎么解决这个问题有什么建议吗?你知道吗


Tags: django用户py图像comtruehttp来源
2条回答

刚刚解决了。。。我需要包括:

http://user.webfactional.com/static/{{userprofile.image}}

userprofile.image.url提供图像的完整url

相关问题 更多 >