Django REST framework – Create endpoints for custom actions

We have talked about how to use Django REST framework to create a RESTful backend for model CRUD.


But sometimes we would like to have some backend endpoints for non-model actions. Here is a very good article written by Jeremy Satterfield about non-model endpoints on Django REST framework.


The Django REST framework offer us a few ways to implement the web service. Some of them are very convenience to use but in return giving you less flexibility. The following example will make use of the APIView class to create a custom GET request end point.

1. Create a new app called custom in your Django project. You can name it whatever u want.

python start app custom


2. Edit <django_root>/custom/

from rest_framework import permissions
from rest_framework.views import APIView
from rest_framework.response import Response
from statsd.defaults.django import statsd

class CustomGet(APIView):
  A custom endpoint for GET request.
  def get(self, request, format=None):
    Return a hardcoded response.
    return Response({"success": True, "content": "Hello World!"})


3. Edit <django_root>/custom/

from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from custom import views

urlpatterns = patterns('',
  url(r'^custom/get/$', views.CustomGet.as_view()),

urlpatterns = format_suffix_patterns(urlpatterns)


4. Include the our newly create custom app in the <django_root>/<project_name>/

# Application definition



5. Also add the route in <django_root>/<project_name>/

from django.conf.urls import patterns, include, url

from django.contrib import admin

urlpatterns = patterns('',
  url(r'^admin/', include(,
  url(r'^', include('custom.urls')),


6. Start the web server.

python runserver


7. Send a GET request using the curl command in shell. You should see the hello world repsone is returned.



Note the the above example is just for setting up the custom endpoint. Please get more ideas about the Django REST framework practice on the Repsonse and Renderer classes in the reference list below.

Done =)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s