Hello
Hello

Reputation: 57

Django : accessing uploaded picture from ImageField

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)

Upvotes: 0

Views: 460

Answers (1)

doniyor
doniyor

Reputation: 37896

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 }}

Upvotes: 1

Related Questions