0

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)

Hello
  • 57
  • 6

1 Answers1

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 }}
doniyor
  • 36,596
  • 57
  • 175
  • 260
  • but the picture has to be referred as **DOMAIN/static/stories/profile_images/.jpg** so **DOMAIN/stories/static/stories/profile_images/.jpg** doesn't work :( – Hello Dec 16 '14 at 14:28
  • @Lumsum "DOMAIN/static/stories/profile_images/.jpg" is not the expected one which is wrong. so you want the "wrong one" ? – doniyor Dec 16 '14 at 14:33
  • Yes, because the picture exists in the "wrong" path (I test with ). I think it's because of the URLConf, so I need the wrong path. – Hello Dec 16 '14 at 14:34
  • @Lumsum then just change your ``MEDIA_URL`` and ``MEDIA_ROOT`` to wrong one, then you will get it. – doniyor Dec 16 '14 at 14:40
  • The picture file exist in ROOT/stories/static/... but to access it in a browser I need to refer to DOMAIN/static... but from {{ x.writer.picture.url }} I got DOMAIN/stories/static/.. so is there any way to truncate the first 8 characters from the url? – Hello Dec 16 '14 at 14:54
  • It's OK now. I created a custom filter. Thank you very much. – Hello Dec 16 '14 at 15:44