DEV Community

Abhijeet kumar
Abhijeet kumar

Posted on

A beginner Guide to Upload Images in a REST API with Django

In modern development, handling image uploads via REST APIs is a common requirement.

Whether you’re building a social media platform, an e-commerce site, or a blogging platform, enabling users to upload images is crucial for any type of applications.

In this tutorial, we’ll go through the process of implementing image uploading functionality in a Django REST API.

We’ll cover everything from setting up Django, creating models, serializers, views, to handling image uploads.

Our mainly concern about to explore the how to setup the Django project to upload images via rest API.

If you need to explore basic Django project setup you can follow this article django project setup in which we have covered basic setup for Django project.

Now let ‘s see each step with example.

  1. We have created our Django project named image_upload and have set up a virtual environment, along with a*** requirements.txt file*** additionally, we have activated our virtual environment named myenv.

  2. Now we have created out app module with image name and which we have added into our INSTALLED_APP in our project’s setting.py file.

    py manage.py startapp image

  1. Next, we installed Pillow and Django Rest Framework we need to add rest_framework to the INSTALLED_APPS list in the settings.py file

    pip install pillow djangorestframework

And it summarized final terminal commands in which we have performed its all ( djangorestframework installation missing).

  1. In this step we have to work on our already create image app module for respectively on models.py, serializers.py , urls.py , admin.py and views.py

Let ‘s break down of each of these files.

In image app module ‘s models.py we have added two field with caption of image as CharField and another with ImageField having directory name as mentioned.

Likewise in image app module ‘s serializers.py we have include all field like id , image with caption however we can added it with (“all”) as commented snippet.

We can also add the out UploadImageModel in out image app module ‘s admin.py so that it will reflect in Django admin user interface for test.

It is our image app module ‘s views.py in which have done two method for post and get respectively using APIView.

Here we have created urls.py for in image app module for and import the view and gave the api name media_upload that have post and get both methods.

  1. In our last step we have to configure directory to store the image and make it accessible for get its images.

In Django, MEDIA_URL and MEDIA_ROOT are settings used to handle media files, such as images, videos, or documents uploaded by users.

MEDIA_URL: MEDIA_URL is a setting that defines the base URL for media files. When a user uploads a file, Django serves it through this URL. In your example, MEDIA_URL = ‘/media/’ sets the base URL for media files to be accessed at **/media/ **on your website.

MEDIA_ROOT: MEDIA_ROOT is the filesystem path where uploaded media files are stored. It’s the location on your server’s disk where Django will save uploaded files.
In your example MEDIA_ROOT = os.path.join(BASE_DIR, ‘media/’)

BASE_DIR refers to the directory where your Django project is located, and ‘media/’ indicates that media files will be stored in a directory named ‘media’ within your project directory.

So, when a user uploads a file, Django will save it to the MEDIA_ROOT directory, and it will be accessible via the URL specified in MEDIA_URL.

For example, if a user uploads an image named example.jpg, it will be saved to *‘media/example.jpg’ **on your server, and it will be accessible via the URL *‘/media/example.jpg’ **on your website.

If the project is running in debug mode (DEBUG is set to True in settings.py), this line dynamically adds URL patterns for serving media files.

Django will serve media files directly from MEDIA_ROOT when the server is running in debug mode.

This means that during development, you can access uploaded media files directly through their URLs without having to configure a separate web server to serve them.

However, this line should not be used in production. In production, you typically serve media files using a dedicated web server (like Nginx or Apache) *or a cloud storage service *(like Amazon S3 or Google Cloud Storage) for better performance and security.

Instead, in production, you would configure your web server to serve media files directly from the specified location (MEDIA_ROOT).

And finally, we have hit these two commands respectively for our database table field creation.

python manage.py makemigrations #  to create migration files

python manage.py migrate   #   to apply those migrations to your database
Enter fullscreen mode Exit fullscreen mode

It is our respectively post and get sample response via postman .

POST —

GET —

Conclusion — Creating a Django REST API for image uploads has been a learning experience. We’ve covered setup, making things efficient, keeping it safe, and testing, giving developers a clear path.

Whether you’re starting out or seasoned, this guide helps you make reliable image upload APIs with Django projects.

Your appreciation or feedback on this insight would be greatly valued, whether it’s a small follow up or leaving a note.

And connect with me on* X handler*** for the tech insights.
Looking forward to engaging with you!**

Top comments (0)