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).
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 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.
When configured correctly, use .url
: item.image.url
. Check the docs here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With