'difference between django and django rest framework
Okay I just finished a YouTube course on Django tutorials and i more or less understand Django to an extent. Now the issue is Django rest framework. Google says its used to create APIs but i just dont get it. if the Django framework can be used to create web applications then what can Django rest framework be used for in terms of web application development. By this question, i mean that is Django framework all you need to create a web app or do you need to add it to the rest framework?. What is the connection between a web app and an API?. Does a web app need an API to function?. I'll appreciate a simple explanation of the difference between the two and and how they connect to each other. Thanks.
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
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.
Solution 4:[4]
Why you need django REST framework after learning django? I think this is the perfect answer for you; Traditional Django does the work, however django REST framework focuses on building APIs, which gives it a lot of additional advantages. If you consider a traditional django app, it is very difficult(almost impossible) to separate the back-end from the front-end. APIs help in separating the front-end from the back-end. Considering the rapid rate of change in JavaScript front-end libraries (React, Vue, Angular etc). This approach of separating the back-end from the frontend is arguably much more future-proof because a back-end API can be consumed by any JavaScript front-end. When the current front-end frameworks are eventually replaced by even newer ones in the years to come, the back-end API can remain the same. No major rewrite is required. Second, an API can support multiple front-ends written in different languages and frameworks for different purposes. It can be consumed by a mobile front-end written in Java or (flutter+dart), a web front-end written in any JavaScript framework, and even iOS app written in Swift. This is not possible in traditional monolithic django approach, however this is very possible and easy in django REST framework.
Solution 5:[5]
Django creates websites containing webpages while Django REST Framework to build web API that allow transfer of data over the world wide web .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vy Nhat Nguyen Lieu |
Solution 2 | Matiiss |
Solution 3 | Bivek Chalise |
Solution 4 | |
Solution 5 | omar ahmed |