If you want to implement Rest API in django and you do not want to user Django Rest Framework , tastypie or any other third party framework then you can follow rules give below to implement REST API almost .
Use HTTP verbs (GET, POST, PUT, DELETE, PATCH)
You should return response in JSON(you can also return in xml)
If you are writing one url to group all of your api related to a object calling by using proper HTTP verbs then you can almost achieve it.
URL /users/
Then REST API for user related operations can be -
GET /users/ To get all users's information.
GET /users/<pk>/ To get a user's information.
POST /users/ To create a user.
PUT /users/<pk> To update a user information.
PUT /users/ To update all user's information at once.
DELETE /users/<pk>/ To delete a user.
DELETE /users/ To delete all users.
class UserView(View):
def get(self, request, *args, **kwargs):
# logic
def post(self, request, *args, **kwargs):
# logic
def put(self, request, *args, **kwargs):
# logic
def delete(self, request, *args, **kwargs):
# logic
import json
from django.http import HttpResponse
def method(arguments):
# logic --
# logic --
data = {'pk': '1'}
response = json.dumps(data)
return HttpResponse(response, content_type="application/json")
that will work but will not support for many other features like authentications, throttling, content negotiation. here you are restricting response for json only but actually it should be based on request headers Accept.
ReplyDeleteThat's why i wrote almost we can achieve this.About restriction on response here we are writing classes and we can smartly use inheritance and other functionality of OOPS to provide dynamic response type or based on request headers accept.
ReplyDeleteAbout features like authentications, throttling, content negotiation we can use features provided by python like decorators and inheritance.
At the end specific feature needs specific implementation that is part of the development.
Im no expert, but I believe you just made an excellent You certainly understand what youre speaking about, and I can truly get behind that.
ReplyDeleteRegards,
Python Training in Chennai|Python Course in Chennai|Python Training
oracle-database-use-xml-db training in chennai
ReplyDeleteThis was an excellent blog. I am happy that I have spent a valid time reading your blog.
ReplyDeleteBest English Classes in Mumbai
Best English Speaking Course in Mumbai
Best Spoken English Classes in Mumbai
Best English Speaking Institute in Mumbai
English Coaching Classes in Mumbai
Spoken English Classes in Chennai
Spoken English Class in Chennai
best baby walker
ReplyDeleteentertainment whatsapp groups
ReplyDelete