Getting Started With Django Rest Framework

Getting Started With Django Rest Framework

Before we get started with django rest framework we need to have the basic knowledge of python, django and how to work with RESTful API's. Django Rest Framework was developed by Tom Christe. It is one of the worlds leading API development frameworks. Django REST Framework is a open-source software and it is used by worlds best companies including Mozilla, Red Hat, Eventbrite, Heroku, and more.

Why to use Django REST framework ?

  • It comes with Web browsable API and it makes developers life simple.
  • It has support for authentication policies including packages for OAuth1a and OAuth2.
  • It supports serialization on both ORM and non-ORM data sources.
  • We can just use regular function-based views if we don't need the more powerful features like generic views and viewsets.
  • It has a great community support in sites like stackoverflow, quora, etc.
  • It is trusted by world's recognised companies like Mozilla, Red Hat, Heroku, and Eventbrite.
  • It is supported by both python2 and python3 Note: I suggest you to use the latest versions of softwares, because it comes with new features and less bugs.

Installation of Django REST Framework

  • It is dependent on django framework though it is automatically installed with django rest framework
pip install django
pip install djangorestframework

Now, let's create and django application named "myproject". If you need help then read article "getting started with django first app".

Steps to configure the django rest framework

  • Add 'rest_framework' to INSTALLED_APPS in our project settings.

INSTALLED_APPS = [
    # ...
    'rest_framework',
    # ...
]
* If you want to use the browsable api and login and logout functionality of Django Rest Framework then add the blo code in project urls.py .

urlpatterns = [
    # ...
    path('api-auth/', include('rest_framework.urls')),
    # ...
]

We have completed the configuration of django rest framework in our django project. To confirm it run the django development server and the in your local browser access url "localhost:8000/api-auth/login/" to see the Django REST Framework login page.

Let's write our first API endoint using Django REST Framework

  • Let's write the simple hello message api endpoint.
  • Create the view named "HelloAPIView" and add below code to it.

views.py

from rest_framework.views import APIView
from rest_framework.response import Response

class HelloAPIView(APIView):

    def get(self, request):
        data = {
            'message': 'Hello World',
        }
        return Response(data)
  • Now, we are ready with our first api view let's configure the view in the urls.py.
# urls.py
from django.urls import path
from . import views

urlpatterns = [
    # ...
    path('api/hello/', views.HelloAPIView.as_view(), name="api_hello"),
    # ...
]
  • Now, access the url in your browser "localhost:800/api/hello/" to see the output of our first rest API endpoint. Do not forget to run the app/project development server. We have setup our first Django REST API application. We will learn more in the upcoming articles.

References:

http://www.django-rest-framework.org/