Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Get ImageField url in view

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).

like image 603
Byteme Avatar asked Apr 02 '13 12:04

Byteme


People also ask

How do I get the full URL of an image in Django?

Also make sure that you have set Django's MEDIA_ROOT and MEDIA_URL parameters and that you can access a photo via your browser http://localhost:8000/path/to/your/image.jpg . Not sure if you can add a QuerySet into the Serializer class. Can you try it with one element only serializer = CarSerializer(car[0]) ?

How do I view images in Django?

How you specify the location of an image in Django is in between {% %}. In between these brackets, you specify static 'images\\Python. png', where Python is the image you want to display which is inside of the images directory in the static directory you create for the current app you are in.


1 Answers

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

like image 65
okm Avatar answered Sep 28 '22 01:09

okm