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 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...

3 minutes read.

Features of Django

Features of Django Offers High Security:- Django is super secure. To prove the feature, we can always take examples of lots of websites which are present worldwide and possess huge traffic. Django is secure because it covers the...

3 minutes read.

Django Channels

Channels is a project that uses Django and adds some functionalities beyond the traditional HTTP by using WSGI and ASGI.  We can use both synchronous and asynchronous requests with ASGI...

5 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 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.

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.

Django Create Superuser

For managing the website, we need an Admin while creating the Django application. In order to create the admin user, create superuser command is used. The superuser is called the...

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 Create App

Django is a Python-based web framework that is based on the Model View Template architecture. In this post, we will go through the installation and setup for our first Django...

4 minutes read.

Django Create Project

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.

Django ORM

Django is a Python web framework that enables easy and quick web development. Django has a handy and powerful feature called Django Object Relational Mapper, also known as Django ORM,...

3 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.

Django Logging

Every application developed has some bugs and errors. It is the responsibility of the developers to maintain the application by making it bug-free and error-free. To debug the application code...

5 minutes read.

Django CMS

Django is one of the most popular open-source Python web development framework. Developers use Django to build a website from scratch. Like WordPress, Django CMS is also a Content Management...

9 minutes read.

Creating View in Django

Creating View in Django    Django views are a key component of the frameworkbased applications.We are using Python function or class at their simplest, which takes up a web query and returns a web response.Views are used to get objects from the server, change objects if needed, render types, return HTML, and much more. Class Based View and Function based Django has two types of views: views based on functions (FBVs) and views based on class (CBVs).Initially, Django began with only FBVs, but then introduced CBVs as a way to model features so we didn't have to write boilerplate (i.e. the same software) code again and again.CBVs would be a way to increase efficiency, so you don't have to write as much code as you can. CBVs are classes of Python at their heart. Django ships with a range of "model" CBVs with pre-configured features that can be reused and often expanded.Then helpful names are given to these classes that explain what kind of functionality they provide.These are often referred to as "universal views" because they provide solutions to specific needs. The classes have a journal View function, or "view" for short, is simply a Python program that uses a web query and returns a web response. This response may include the HTML content of a web page, or a redirect, or a 404 error, or an XML file, or an image, etc. Example: You use a view to create a web page, remember that you need to connect a view to a URL to see it as a web page. Simple View We will create a simple view in myapplication to say “Welocome to django application” See the following view – from django.http import HttpResponse ...

4 minutes read.

Django Framework

Django is a popular high-level open-source web development Python framework based on follows Model View Template (MVT) architecture that allows rapid development. It. It provides common functionality like Admin Interface,...

3 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.

How to Implement Multiple User Types with Django

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...

3 minutes read.

Django Filters

Django is a high-level python based web framework. Django is mostly used as a backend engine for websites. Nowadays, every website communicates to a database to fetch all the information...

3 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.