1

I have a profile database for users in which I save their profile image. I am using DRF to create API's for this. Now when I save the profile data, it return JSON as:

{
    "branch": "CSE",
    "year": 2014,
    "image": "static/profile_images/abhishek.jpg"
}

this data gets saved in database and even the image get uploaded to the desired folder. Now I want to access this image in the frontend(I am creating an android app in Ionic), so that the profile pic is viewed in the app's dashboard. How do I do it? At present simply going to the saved path is not working.

7
  • either your images need to be hosted or you need a way to retrieve the image you stored....where is "/static/profile_images/abhishek.jpg"? Commented Nov 23, 2017 at 16:56
  • It's in the root folder. Commented Nov 23, 2017 at 16:58
  • if its not hosted ... you need an endpoint on your server-side code that retrieves the image and sends it back Commented Nov 23, 2017 at 16:59
  • How do I do that? Commented Nov 23, 2017 at 17:00
  • 1
    Have you checked this: docs.djangoproject.com/en/1.11/howto/static-files/…. Also, why are you saving uploaded images in static? Django convention is to save it in media directory. For a personal project it's okay, but down the line if you decide to share your project with someone or have someone collaborate with you on it, then it would lead to many confusions. Commented Nov 23, 2017 at 17:56

1 Answer 1

3

I found the answer what I was looking. Actually in Django files can be partitioned into two:

Static files: those files used in the development, creation and rendering of the website or app. Things like stylesheets, scripts, fonts etc.

Media files: those files that are uploaded during the normal usage of the website or application such as images, pdfs, videos etc.

So to have upload functionality one needs to add the following in the settings.py

ENV_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(ENV_PATH, '<folder-name>')
MEDIA_URL = '/media/'

and in urls.py add:

urlpatterns = [
    .......
    your urls
    .......
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.