Django DRF

Django Rest Framework, which is also referred to as DRF, is a very powerful application to build RESTful APIs in a Django application. DRF uses the Django framework. Similar to Django, DRF is also an open-source network.

We can create APIs with minimal effort and quick time with Django DRF. DRF’s documentation is considered by many to be better than Django. We can make highly customizable APIs with DRF, which supports authentication, authorization, pagination, sorting, etc.

Why DRF?

  • We can integrate authentication with technology like OAuth 1a and OAuth2.
  • Serialization is also included, which can support ORM.
  • Django DRM has great community support in case we are stuck with a problem.
  • Django Rest Framework is highly customizable.
  • Many giant tech companies are using it, like Mozilla, Heroku, and RedHat.

What is an API?

API stands for Application Programming Interface. API is used to communicate between two applications. It is software that is used by another software to provide some information.  In Django, API acts as a mediator to other applications like any data source, android, web browsers, etc.

The API is mainly used in a backend to fetch the data from other sources like a server or a database and provide it to the application. The data which API retrieves can be in many formats like XML, JSON, etc., but JSON is very common and easier to work with as a web developer. 

There are many APIs example, on the internet, there is an API for mostly all the tasks,  github has an API for the repositories, there is an API for random names, Google has an API for integrating Google Maps.

RESTful API

REST stands for Representational State Transfer. REST is an architectural way of designing network applications. Any data that the client is retrieving through REST can be fetched/edit/delete. REST defines every operation with a specific URL.

A RESTful API can be called a mixture of REST and API, which acts as a mediator between two computers over a web service. The difference between an API and RESTful API is that RESTful API communicates on a RESTful Web Service.

Requirements

We need to have certain programs and software to use the Django Rest Framework. The requirements are:

  • Python (3.5 or higher)
  • Django (2.2, 3.0, 3.1)

Installation

We can install the Django REST framework by using python package manager pip. To install the DRF, use the command below:

pip install djangorestframework

Now, we have to specify to Django that we are using the REST framework by adding it into the INSTALLED_APPS section of the settings.py file.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'talk',
    'rest_framework'
)

We can also add the following code to the urls.py file:

urlpatterns = [
    ...
path('api-auth/', include('rest_framework.urls'))
]

We have successfully installed and integrated the Django REST framework into our project.

Usage

Now, we will see how to use the DRF in the application.

First of all, we have to configure the settings for REST_FRAMEWORK in our settings.py file.

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

Add the following code in the urls.py module:

fromDjango.urls import path, include
fromDjango.contrib.auth.models import User
fromrest_framework import routers, serializers, viewsets




classRest_Serializer(serializers.HyperlinkedModelSerializer):
classModel:
model = User
fields = ['url', 'username', 'email', 'is_admin']


classREST_View(viewsets.ModelViewSet):
users = User.objects.all()
new_serializer = REST_Serializer


router = routers.DefaultRouter()
router.register(r'users', REST_View)




urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))

Related Topics

Django Admin

To manage the content of the website, e.g., adding and deleting posts, web developers need an admin portal on the website. The admin portal allows trusted users, also knows as...

3 minutes read.

Django Tutorial

Django Introduction It is a web application framework written in a python programming language. It based on the MVT( Model View Templet) design pattern. It takes a short time to build an application...

2 minutes read.

Django vs Laravel

Django Overview Django is a Python Programming Open Source framework for web development. It was released in 2008 and was developed by two web developers named Adrian Holovaty and Simon Willison....

4 minutes read.

Life Cycle of Django

Life Cycle of Django Request - Response life cycle in Django  Thebasic principle of http protocol is the client sends a request to the server based on the request data, and the server sends...

3 minutes read.

Django Template System

Django Template System Django allows Python and HTML to be divided, the Python goes into views, and HTML goes into templates. Django relies on the render feature and the language of the Django Model...

5 minutes read.

Django CMS vs WordPress

Both Django CMS and WordPress belong "Self-Hosted Blogging" or "Content Management System (CMS)" category, that is used to manage digital content. Bloggers usually use these platforms. Django CMS Django is one of...

4 minutes read.

Django All Auth

For any web application, user registration and authentication are the essential parts. In Django, there are several applications like django-registration-redux to perform these tasks, but the major drawback is that...

6 minutes read.

Django Model

Django Model A template is a class representing a table or collection in our DB and where each class attribute is a table or collection area. In the app / models.py, models are...

6 minutes read.

Django and React

This article will discuss the overview of Django and React, their advantages, applications, and most importantly, how to implement react in Django. What is Django? Django is a free and open-source web...

6 minutes read.

How to create an app in Django

Create an application in Django Django is famous for its unique and fully managed application structure. For each functionality, an application can be created as a completely independent module. This article will take...

2 minutes read.

Django Authentication

Django is an open-source free to use web framework which is based on Python. Django uses Model-View-Template as its architecture. The final version of Django was released in 2008. Django...

4 minutes read.

How to connect Django with Mysql

Any web application contains some data that comes from the input of the end-users. Thus, the database is required to handle those data. Several databases could be used to connect...

3 minutes read.

Django Stack

Django is a popular Python web application framework that adheres to the "batteries-included" tenet. Batteries-guiding included idea is that common functionality for creating web applications should be provided with the...

5 minutes read.

Django CSRF

Django is a high-level python based web framework. Django is open source and free to use for all. Django provides many features to web developers like scalability, security, rapid development,...

4 minutes read.

Best Django courses

In this tutorial, we will see various courses in python Django available on the internet. Django has become popular these days, so courses on it are also increasing day by...

7 minutes read.

Django REST Framework

Django REST Framework (DRF)is an open-source, powerful, and flexible toolkit used to build RESTful web APIs. It is built upon the Django framework. DRF increases the development speed. Django and...

4 minutes read.

Django Installation

Django Installation Django development environment consists of installing and setting up Python, Django, and a Database System since Django deals with a web application. Below the statement will guide you through installing Python 3.8.0 and...

2 minutes read.

Django Frontend

Django Django is a framework that can make web applications more accessible and faster. Django is an open-source collection of python libraries, and Django can be used for both the front...

3 minutes read.

Django Image Upload

Django is a python based web framework that uses Model View Template as its architecture. Django provides really quick web development with its functionalities. While we are building a website,...

3 minutes read.

Django Messages

Django Framework Django is a framework that can make web applications more accessible and faster. Django is an open-source collection of python libraries, and Django can be used for both the...

6 minutes read.