Django : accessing uploaded picture from ImageField

微笑、不失礼 提交于 2021-02-20 04:38:06

问题


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

I'm using ImageField to save profile pictures :

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

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

Then I refer it in HTML like this :

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

Which directs to

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

Instead of

ROOT/static/stories/profile_images/.jpg

Any ideas?

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


回答1:


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

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

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

{{ x.writer.picture.url }}


来源:https://stackoverflow.com/questions/27503989/django-accessing-uploaded-picture-from-imagefield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!