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 manage.py start app custom

 

2. Edit <django_root>/custom/views.py.

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/urls.py.

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>/settings.py.

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'custom',
)

 

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

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

from django.contrib import admin
admin.autodiscover()

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

 

6. Start the web server.

python manage.py runserver 0.0.0.0:8000

 

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

curl http://127.0.0.1:8000/custom/get/

 

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 =)

Reference:

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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