Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of using REST API framework over simple URL and view creation in Django?

It might be a silly question for many, but why can't I instead

  • Create a view in django that takes a request and returns HttpResponse in, say, JSON format
  • Map the view to a URL
  • Hit the URL from my browser or another server and use the result?

Thanks.

EDIT - Two approaches: Import some djangorestframework or tastypie and build an api in my application which will throw json responses VS building a view and tell it to return json response. Is there any huge advantage of using the first one?

like image 788
Ketan Avatar asked Feb 04 '16 10:02

Ketan


People also ask

What is the advantage of Django Rest Framework?

Main advantages of Django REST framework: Simplicity, flexibility, quality, and test coverage of source code. Powerful serialization engine compatible with both ORM and non-ORM data sources. Pluggable and easy to customise emitters, parsers, validators and authenticators.

Why use Django Rest Framework over Django?

Why Django REST framework? Django REST framework (DRF) is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier. Django REST framework is based on Django's class-based views, so it's an excellent option if you're familiar with Django.

What is difference between Django and Django Rest Framework?

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.

What is the difference between Django and REST API?

REST is the general framework, while Django Rest Framework DRF is a specific REST framework used in Django. It's easy to make Web APIs with Django because of its modular and customizable armature. Django emphasizes security by furnishing defense against common SQL injection and cross-site request forgery attacks.


1 Answers

I think you could make the same argument about any extension library. It just depends on how much you want to rebuild and what the existing library has that would be beneficial to your project.

Many times I've created custom endpoints without an API library when working with ajax requests inside my project. For that instance, using an API package is overkill. But for having a full API server, Django rest framework offers a lot of functionality.

Sure, you can make views that do what you suggested. But at some point are you going to want to authenticate through an HTTP request? Are you going to want to filter? Are you going to want to make permissions, or just have all endpoints open? Are you going to want to protect against CORS?

You can kind of go down the list of all the features of an API library and ask these questions about what you want to accomplish with your project. If you're working with any type of external application and your django project is just for an API server, usually it's best to go with Rest Framework. If you just have some one-off endpoints to receive ajax requests, usually you just want to build custom endpoints.

like image 127
awwester Avatar answered Oct 29 '22 17:10

awwester