Django ImageField url not relative to MEDIA_ROOT

4,576 views
Skip to first unread message

pielgrzym

unread,
Apr 2, 2009, 6:18:21 AM4/2/09
to Django users
Hi there,

Before explanation here is the link to problematic code:
http://wklej.org/hash/968a35feef/


If one creates a model with imageField and uses it via admin
everything is just fine - model instances have the image field with
correct url:

photo = Image.objects.get(pk=1)
photo.image.url
'/media/blabla/photo.jpg'

If I upload the file manually, not via admin interface the image.url
is an absolute path to the image file:

photo2 = Image.objects.get(pk=1)
photo2.image.url
'/home/user/django_projects/myproject/media/blabla/photo.jpg'

Could someone explain me what am I doing wrong?
This happens on django 1.0 and latest django svn snapshot :(((

pielgrzym

unread,
Apr 2, 2009, 6:38:47 AM4/2/09
to Django users
Thanks to pagenoare on #django-pl there is some progress:

If I create a modelform, and instead of manually invoking model
instance and saving it, I just do:

new_image = form.save()

Things work like expected. Why? :)

SashaN

unread,
Apr 17, 2009, 11:00:39 AM4/17/09
to Django users
I'm hitting same problem. It seems to me it is a bug in django.

Christian Berg

unread,
Apr 17, 2009, 11:03:58 AM4/17/09
to django...@googlegroups.com
Hello!

The ImageField and FileField URLs are relative to MEDIA_URL
It's important to skip the leading slash, and add an slash to the end of
the path Attribute.
example:

image = models.Imagefield(path="images/")

In the Template use {{MEDIA_URL}}{{image.url}}


On Fri, 17 Apr 2009 08:00:39 -0700 (PDT), SashaN <anedv...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages