Django:从ImageField访问上传的图片 [英] Django : accessing uploaded picture from ImageField

查看:56
本文介绍了Django:从ImageField访问上传的图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用HTML引用上传的图片时遇到了问题.

Hi I'm having a problem about referring to uploaded images in HTML.

我正在使用ImageField保存个人资料图片:

I'm using ImageField to save profile pictures :

picture = models.ImageField(upload_to='stories/static/stories/profile_images/', blank=True)

如您所见,它将上传到< App文件夹>/static/stories/profile_images/

As you can see, It'll upload to <App folder>/static/stories/profile_images/

然后我像这样在HTML中引用它:

Then I refer it in HTML like this :

<div class="profilepic" style="background-image: url('/{{ x.writer.picture }}');"></div>

指向

ROOT/stories/static/stories/profile_images/.jpg

ROOT/stories/static/stories/profile_images/.jpg

代替

ROOT/static/stories/profile_images/.jpg

ROOT/static/stories/profile_images/.jpg

有什么想法吗?

(我正在尝试截断前8个字符,但没有内置模板过滤器可用)

(I'm trying to reverse-truncate the first 8 character, but there is not built-in template filter available)

推荐答案

由于您的 upload_to stories/static/stories/profile_images/,因此它显示正确的路径:

since your upload_to is stories/static/stories/profile_images/, it IS showing right path:

ROOT/stories/static/stories/profile_images/.jpg

还有另一件事,您需要 .url 来获得带有其路径的图片

and one more thing, you need .url to get the picture with its path

{{ x.writer.picture.url }}

这篇关于Django:从ImageField访问上传的图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆