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

时间:2012-09-25 06:49:52

标签: python django settings models imagefield

我正在尝试上传用户生成的图片,然后在我的django网络应用中显示。图像上传到服务器但我无法显示它。

models.py

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

settings.py

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

举个例子,假设我上传了一个名为Finland.gif的文件。我可以看到上传的文件。但是,当我查看源代码时,我将图像的来源看作“www.foo.com/accounts/profile/images/Finland.gif”,而不是静态图片网址,应该是“http://user.webfactional .COM /静态/图片/ Finland.gif”。关于如何解决这个问题的任何建议?

2 个答案:

答案 0 :(得分:2)

userprofile.image.url为您提供图片的完整网址

答案 1 :(得分:0)

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

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