Django: Get ImageField url in view

Byteme picture Byteme · Apr 2, 2013 · Viewed 57.9k times · Source

I am trying to get ImageField absolute path in Django view so I can open the image, write on it something and then serve it to user.

I have problem getting absolute path for the image which is saved in media folder.

item = get_object_or_404(Item, pk=pk)
absolute_url = settings.MEDIA_ROOT + str(item.image.path)

I get error, that item.image does not have path (The 'banner' attribute has no file associated with it.). item.image is ImageField and I'd like to know how to get absolute path of the image saved in image field (within Django view).

Answer

okm picture okm · Apr 2, 2013

When configured correctly, use .url: item.image.url. Check the docs here.