问题
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