Introduction to Django REST Framework
Just the way we humans interact with computers with the help of a user interface, web applications or software programs also need to interact with each other. In order to do so, a different kind of interface is created. Before diving right into understanding what Django REST Framework is and how it works, here’s the table of contents for this Django tutorial in case you want to jump right into a particular topic.
Watch this video on ‘Python Django Tutorial’:
Without further ado, let’s get started.
What is API?
From ordering food, or booking a hotel room, to downloading a software online, all this interactivity is made possible using something known as API or Application Programming Interface. In other words, API is nothing but an interface that other software uses to access data or other applications.
In simple terms, API is a way software programs interact with each other. Developers create an API on the server and allow clients or end-users to interact with it.
We know that a website uses the URL address to make a server call and pull up a webpage in a browser. APIs also facilitate server calls but in a simpler manner. APIs offer a standard set of instructions, using which developers, software applications, and sites can access web services or assets of a web application.
But the answer to the question, ‘How to access web services?’ was only SOAP (Simple Object Access Protocol) for a while. Unlike the acronym suggests, the protocol isn’t that easy. SOAP used to perform communication using the XML file structure exclusively. In order to work with SOAP in JavaScript, developers had to write a ton of codes, just to perform a simple task.
So, there was a need of a simpler alternative to SOAP. That is when REST API came into the picture.
Get 100% Hike!
Master Most in Demand Skills Now!
Watch this video on ‘Django Interview Questions’:
What is REST API?
REST stands for Representational State Transfer (REST). REST API uses a simple URL to make requests, instead of using XML. The primary rule of REST states that one should be able to get back a set of data when linked to a specific URL. In other words, a URL is used to send a request and to get back a response.
REST can use different HTTP verbs such as GET, POST, PUT, and DELETE to perform tasks. Unlike in SOAP, in REST-based web services, we can obtain outputs in a form that is easier to parse within the language, such as, JSON (JavaScript Object Notation) or CSV (Comma-separated Values).
Now, the question is ‘How do we create a REST API?’ Well, there are many options to build a REST API. Django REST Framework is one of the best among them. Let us discuss what Django REST Framework (DRF) is and why we are going ahead with it.
Let us move ahead and take a look at the factors that make DRF so popular.
Why Django REST API?
Here are a few key points why developers choose Django REST Framework:
- DRF is easy to use, yet powerful and sophisticated at the same time.
- It provides unique features to build web-browsable APIs.
- It provides a number of out-of-the-box authentication schemes that canidentify the credentials that the request was made with.
- It offers the serialization functionality that supports both ORM (Object-relational mapping is a technique that converts data between incompatible type systems using object-oriented programming languages) and non-ORM data sources.
- DRF provides the feature of customizability. If we don’t need more powerful features, we can just use regular function-based views.
- DRF is backed up by a set of extensive documentation and a great community.
- It has gained popularity also because the web development process is done with Python programming language.
Alright, let us discuss what Django REST Framework is and the structure of Django REST API.
What is Django REST Framework?
Django REST Framework or DRF is just a powerful yet easy-to-use platform that allows a web-browsable version of API. DRF is built on top of Django. It provides a set of powerful tools to make the RESTful API development process easy.
Understand the simple differences between SOAP and REST, pick the one that suits your data needs.
Alright, let us go ahead and discuss the installation process of Django REST Framework.
Django REST Framework Setup
Prerequisites for Django REST Framework
- Python
-
- Check if Python is installed in your system, by running the following command in the command prompt:
python --version
- Django Web Framework
-
- Check if it is installed in your system, by running the following command in the command prompt:
django-admin --version
Installation Process for Django REST Framework
Run the following command in the command prompt to install Django REST Framework:
pip install djangorestframework
Django Serialization
The first part in the process of building an API is to provide a data serializer in DRF. A Django serializer converts complex data from models and querysets into the JSON format.
Django serializers also work similarly to Django’s Form and ModelForm classes. While building a Django REST API, we provide the Serializer class that gives us a powerful and generic way to control the output of our responses.
Django serializers provide useful shortcuts for creating serializers that deal with model instances and querysets. One magical thing about Django REST Framework is that Django serializers also provide deserialization, which allows parsed data to be converted back into complex types, after first validating the incoming data.
Requests and Responses in Django
Two of the most essential building blocks of Django RESTful API are adding request objects and response objects.
Request Objects
DRF provides a request object that extends the regular HttpRequest, which provides more flexible request parsing.
The primary functionality of a request object is the request.data attribute, which is similar to request.POST, but more useful for working with web APIs. Let us see the difference between request.POST and request.data.
- request.POST can only handle the form data. It only works for the ‘POST’ method.
- data can handle arbitrary data. It can be used for ‘POST’, ‘PUT’, and ‘PATCH’ methods.
Response Objects
DRF also provides a response object that takes unrendered content and uses content negotiation to determine the correct content type to return to the client.
Django Class Based Views
Django class based views are used in place of normal function based views, when the function based views start containing lots of code. There are certain key points about Django class based views that we need to remember before moving ahead.
- Class based views promote the reusability of common functionalities.
- Class based views allow us to write more concise code.
- Class based views allow us to encapsulate the common functionality for inheritance.
Django Routers
In Django REST Framework, a router allows the automatic URL routing. In other words, it provides us a simple, quick, and consistent way of wiring our view logic to a set of URLs. So, instead of defining the URL pattern for each view in a ViewSet, we can just use a router to do it for us.
In this Django tutorial, we have learned what a REST API is and why do we use APIs and REST APIs. It also discussed topics such as Django REST API, Django serializers, Django class based views, and more. If you’ve found these concepts interesting and want to get in-depth knowledge on Django Web Framework and Django RESTful API on your own, head on to Intellipaat’s Django Course structured by technical experts.