0

I would like to know if it is possible to build a REST Api using only Django (without Django REST).

I have following code in my views.py my Django (no REST Django)

from django.http import HttpResponse, JsonResponse

def get_something(request, object = None):

    dummyDict = {'Debug':object}

    return JsonResponse(dummyDict)

ulrs.py

 url(r'^(?P<object>\w{1,50})$', views.get-something, name = "get-something"),

Can this work as REST API? I tried testing using curl and I get following answer from my django server:

HTTP/1.0 200 OK
Date:
Server:
X-Frame-Options: 
Content-Type: application/json

{"Debug": daodoaw}
3
  • 5
    Sure you can, but as you scale IMO you'll face problems that DRF has already solved. Commented Mar 12, 2017 at 12:17
  • @nik_m thank you for the answer! Commented Mar 28, 2017 at 11:15
  • I suggest you to use Flask Commented Oct 26, 2018 at 8:15

3 Answers 3

2

You may do that though you'll have to add a lot of things to make an API RESTfull.

Your example is already missing proper response code for PUT / POST / PATCH / DELETE and doesn't respond correctly to OPTIONS

Sign up to request clarification or add additional context in comments.

Comments

0

Ofcourse you can do these things with Django, but the advantage of django rest is that it is specifically created to handle api creation and management, Thus using django rest will be much more effective. When you look at the documentation of django rest, you could see that viewsets like ModelViewSet, GenericViewSet which are capable of handling any type of requests(POST, PUT, PATCH, etc...) in a two or three line code. Meanwhile while you using django you have to specify each and every case. Its just a case where DRF is more preferred). What I try to say is, DRF is more handy in the case of API Creation and has a very good documentation in their official website

If you have to know about any specific part, please comment below Thanks

Comments

0

Using Django is not a crime! We are given the liberty to use either DRFor Django.

Here are some of the situations when you can think of using DRF:

  1. Let's imagine, you have developed a web application with django. What would happen if your client or you want a mobile version (android/iOS version) of your application? As multi-platform development is becoming the norm, using DRF can become crucial to reuse the same backend for both a mobile app and a web app.

  2. Another comprehensive reason to use DRF is its super easy serialization facility.

  3. If you want to segregate your front-end from Django provided templates, you are free to use modern javascript frameworks such as Vue, React or Angular for better design and to be dynamic as much as possible with your REST APIs.

  4. When you use DRF, you are given ready-made view classes (ApiView, GenericAPIView, Viewsets) that are only a few lines of coding and able to handle all sorts of requests such as (POST, PUT, PATCH, etc...).

NOTE: Definitely, you can use Django depending on the project you are handling. In the case of building a web application that doesn't need to be accessed from other platforms, I would not use DRF. It entirely depends on the requirements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.