[Solved]-Difference between Django and Django rest framework

18👍

Your question was the same question I used to ask my senior at work when I first learned about Django and Django Rest Framework.

The answer I got was that Django Rest Framework helped you create API endpoints faster like one of the comments with the sample codes. I suggest you should read the comments by J. Hesters and C S here. However, I was not completely satisfied with the answer too. After working with them for a while, here are what I think:

Technically, let’s forget about Django Rest Framework. You can use Django only to build a fully functional web app without using any frontend frameworks, such as React, Angular, etc. Doing so, you have used Django for both backend and frontend. Actually, doing like this, you do not have the concept of backend and frontend. Your web app is just your web app, and that is it.

However, if you want your frontend to look fancy with complex CSS decoration, you may want to consider using frontend frameworks (React, Angular). Of course, you can use Django alone to make your frontend looks fancy too, but you have to write a lot of code to do so, and Django template is not popularly used comparing to frontend frameworks.

Now, let’s say you want to use a frontend framework. If you do not have a REST API, and your frontend code try to request data from one of your URLs, you will get a string data or an HTML page like what you get from using curl, it is not useful to get that data for your frontend. However, if you have a REST API, your backend data will be serialized in the way that your frontend code can understand and deserialize the returned data into some data objects like dictionary which you can use right the way.

Now, Django vs Django Rest Framework. You can use Django alone to make REST APIs, but you have to write more code and do more design like one of the comment above showing in the example. By using Django Rest Framework, you can write less code and reuse your code better.

Also, I think you may want to look into the difference between API vs REST API. They are using interchangeably, but they are not the same. For example, when you are using Django, you are using the Django APIs. REST(ful) API which is just one type of API is used for client-server web developments.

Also, libaries and APIs are similar but not the same.

6👍

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django.

Django Rest Framework makes it easy to use your Django Server as an REST API.

REST stands for "representational state transfer" and API stands for application programming interface.

You can build a restful api using regular Django, but it will be very tidious. DRF makes everything easy. For comparison, here is simple GET-view using just regular Django, and one using Django Rest Framework:

Regular:

from django.core.serializers import serialize
from django.http import HttpResponse


class SerializedListView(View):
    def get(self, request, *args, **kwargs):
        qs = MyObj.objects.all()
        json_data = serialize("json", qs, fields=('my_field', 'my_other_field'))
        return HttpResponse(json_data, content_type='application/json')

And with DRF this becomes:

from rest_framework import generics


class MyObjListCreateAPIView(generics.ListCreateAPIView):
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]
    serializer_class = MyObjSerializer

Note that with DRF you easily have list and create views as well as authentication.

2👍

This is the perfect answer for you: Django REST framework focuses on building APIs, which gives it a lot of additional advantages over traditional Django.

It is possible to build APIs with Django only but using Django Rest Framework makes it fairly easy.

APIs are important because they help us to build a separate back-end completely independent from the front-end. If you consider a traditional Django application, you realize the front-end and backend are sort of tied together forming the website/webapp.
Let’s say you want a mobile app or a desktop app and not a webapp/website what will you do? APIs are used to achieve this.

An API can support different/multiple front-ends written in different languages and frameworks for different purposes. It can support a mobile front-end written in Java or flutter, a web front-end written in any JavaScript framework(React, Angular, Vue), and even an iOS app written in Swift.

So by having a separate backend/API, you can always use it to create the type of application you want by just creating your frontend and integrating it with the API.

1👍

Django is a web server built over python while django-rest is a package for django servers. If you only use django without using django rest framework, most probably you will be creating sites by ‘server rendering’ but if you are using django-rest in your django application you will be creating apis that will be later consumed by other frontends like react or vue.

1👍

Django framework is a backend development framework that is use to develop backend application that render to web pages while Django rest framework is API that supply server data to other frontend application without them thinking about the backend. A good senario is a banking app want to get account balance it will query the django rest API to get data or summit data. The reason for django API is to feed other external application that may need data for their smooth operations.

1👍

To answer your questions, let’s take them one after the other:

"is Django framework all you need to create a web app or do you need to add it to the rest framework?"

Django framework is enough, and in the context of a web app, it is all that is required to create one.
Rest framework,eg. Django Rest Framework, is a library that can be used to make it simpler when building rest API’S.

"What is the connection between a web app and an API?."

To answer this question, it’s best to understand what an API is. In simple terms, consider an API as an interface that defines a set of rules regarding how other applications can make use of your application. These rules are presented in the form of endpoints(URI’S) to which requests can be made to render some data. You can find useful resource here link.
A web app can be presented in the form of a REST API to serve some particular data(location data, payment processing) which:

a). other apps can consume. eg, PayPal’s API can be integrated into app for payment processing

b). other frontend frameworks can be used to make requests to the API endpoints to render the frontend interface of the requested data. This makes use of the principle of the separation of concerns (frontend and backend)

Does a web app need an API to function?

Best answer i can give is, it depends.
Consider this scenario:
Say you are building an app to handle logistics and delivery, would you build afresh the "maps" feature in your logistics app or you will rather consume the google maps API to handle the maps feature in your app.

In short, an app may or may not need an API to function and this is dependent on architecture.

-1👍

Django creates websites containing webpages while Django REST Framework to build web API that allow transfer of data over the world wide web .

Leave a comment