Django REST Framework | Creating REST APIs in Django

Sagar Patil
3 min readAug 4, 2022
Source: Freepik

API:

API stands for Application Programming Interface, a software intermediary that allows two applications or programs to talk to each other. It takes the request from the user and sends it to the service provider and then again sends the result generated from the service provider to the desired user. A programmer can make use of various APIs to make its program and development easier, simpler and efficient.

A real-life example of APIs can be, that each time we use an app like Facebook, Instagram, or Twitter to check the latest post and update whom we follow or post something from our side or check the weather on our phone, you’re using an API (i.e. where our application needs to fetch data or request some service from someone).

There are different types and subtypes of APIs, among them web APIs are the widely used APIs, for example, REST & SOAP. In this article, we will see how we can create REST APIs in Django. Before this, let's have an overview of the Django Rest framework.

Django REST Framework

Django REST framework is a powerful and flexible toolkit for building Web APIs. It’s a wrapper over the default Django Framework, basically used to create APIs of various kinds. There are three stages before creating an API using the REST framework:

  • Converting a Model’s data to JSON/XML format (Serialization),
  • Rendering this data to the view and
  • Creating a URL for mapping

Implementation

Let's start with the actual implementation. First, we will create a Django project named “rest_demo” and an app named “sample_app”. To create a Django project and app you can refer to my article “Creating Your First Django Project for Backend Web Development”.

Django project file structure

settings.py: Add rest_framework, sample_app to INSTALLED APPS

models.py: We will start by creating model for student as StudentModel which has student id as sId, name of student studentName and class of student as studentClass as the three fields.

sample_app/models.py

serializer.py: Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. We’ll declare a serializer that we can use to serialize and deserialize data that corresponds to the model object.

sample_app/serializer.py

urls.py: We’ll add API URL in urls.py file.

urls.py

views.py: Now, we’ll create viewset to handle requests and render data into frontend.

sample_app/views.py

We are done with the code. Run the commands given below:

python manage.py makemigrations
python manage.py migrate
python manage.py runserver

API Testing using Postman:

We’ll use postman, which is a great tool for API testing.

Post Request:

Sending data using POST method

As we can see above data is sent to backend via POST method from the url “http://127.0.0.1:8000/student”. When the server receives data it sends the message “Added successfully!!”.

GET Request:

Retrieving all data using the GET method

Similarly, using GET method from the url “http://127.0.0.1:8000/student/”, our api fetch the data from the server and renders it to frontend.

Conclusion:

Thus we can see our APIs are executing successfully. Here we come to the end of the implementation and testing of API in Django using REST framework. Do let me know if you find this article helpful.

The code of this implementation is available here.

--

--

Sagar Patil

Data Scientist at Jio 🖥️📊 | Building Data Platform and Products for Jio | Google Cloud Engineer | Technology & Business Enthusiast