Sylph
Sylph

Reputation: 151

Retrieve image with Django Rest

I'm a newbie in Django, and I want to ask how to fetch images with Rest Api for Django.

Currently I have a model with an ImageField and I have no trouble uploading images, or fetching the json for my item but when it comes to fetching it I don't have any clue how. I save the images in src\image\ inside the django app root folder. How do i fetch the image with an url like: localhost:8080/src/image/image1.jpg?

Upvotes: 0

Views: 541

Answers (1)

Enthusiast Martin
Enthusiast Martin

Reputation: 3091

Please, include your urls.py.

Do you have MEDIA_ROOT correctly configured? This is the location where your files are stored/uploaded.

But I assume you'd need to specify endpoint for your media files.

something like this :

from django.views.static import serve

urlpattenrs = [
     url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]

After that, path is the location of your file which you used to store it ( relative to MEDIA_ROOT) .

You can now fetch your files using media endpoint + location of your file in MEDIA_ROOT directory ( you can call the endpoint like you want, i like media )

Upvotes: 1

Related Questions