Shrihari Shastry Shrihari Shastry - 10 months ago 111
Python Question

How do I route the POST,PUT and GET to different business logic on Django-REST framework?

I have started working on django-rest framework.... I selected this framework because I wanted to integrate the same api's with a mobile application(android). I had done this before with nodeJs(Express) , I used to write GET,POST,PUT and DELETE request for the same url and write business logic in it. How do i implement the same with DjangoRest....

How do I route the PUT or POST request to business logic and give a response like I do in ExpressJs

Something like below:

module.exports = function (router) {

router.get('/demoEndPoint', function(req,res){
/*business logic here
res.send('hello world');
});'/demoEndPoint', function(req,res){
/*business logic here
res.send('hello world this Post Request');
router.put('/demoEndPoint', function(req,res){
/*business logic here
res.send('hello world put request');


Answer Source

To create single api endpoint that handles different types of requests one should override the methods to implement custom logic. By default there are be predefined functions for each http methods GET,PUT,POST,PATCH and DELETE. Refer to the code block below for an example.


url(r'^api-endpoint/', apiView.as_view(), namespace='rest_framework')),


class apiView(viewsets.viewset):
    def get(self, request):
    def post(self, request):
    def put(self, request):