Django Rest Framework : How to create an API easily

April 2019

Discover my new eBook and get 20% on your purchase with this coupon code : 6XGMDHWX
Django Admin ultimate guide

Introduction to Django Rest Framework

Django Rest Framework is a library which will help us to setup and develop an API really easily. With this API, our Ionic project will be able to communicate with our Django BikeBackend to get, create, modify, delete our data (using Http calls thru this API).

Django Rest Framework builds REST API, which means all requests will be using standard HTTP requests (GET, CREATE, DELETE,UPDATE) and our HTTP responses will be json objects, which is perfect since it is the standard format that our Ionic project will expect.

Install Django Rest Framework

To install the framework, will will add the following line in our requirements.txt file

djangorestframework
django-filter

The first line is the Django Rest Framework itself, and the second line is not mandatory but we will use it later to filter our data. Then in our settings.py file, in the INSTALLED_APPS section, we add the line:

'rest_framework',

To install the packages, don't forget to launch in your terminal:

 pip3 install -r requirements.txt

Now still in our settings.py file, we will add a new dictionary specific to our REST_FRAMEWORK to describe some basic rules:

#Rest
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
       'rest_framework.authentication.BasicAuthentication',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 20, 
}

We are now ready to use the API. In our urls.py file add:

from rest_framework.routers import DefaultRouter
router = DefaultRouter() 

and in the urlpatterns dictionary we define the route like this:

path('', include(router.urls)),

At this time, the urls.py file should look like this:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf import settings
from django.conf.urls.static import static
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
urlpatterns = [
    path('', include(router.urls)),
    path('jet/', include('jet.urls', 'jet')),  # Django JET URLS
    path('admin/', admin.site.urls),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Now if you open your browser to http://127.0.0.1:8000/ you should see the default Django Rest Framework page:

Create API endpoints with Django Rest Framework

API endpoints are the urls which we can call to get or send data. In our bikebackend root directory, we will create a new directory named api in which we will develop the API (you can name the directory whatever you want, but i keep it simple since it is an API i call it api).
In your terminal launch the command:

mkdir api

go inside the api directoy, and as usual since it is a python directory, i create an empty __init__.py file

touch __init__.py

Now we will create a urls.py file to enumerate all endpoints that will be available.

from django.urls import path
from django.conf.urls import include
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
urlpatterns = [
    path('', include(router.urls))
]

To declare these endpoints to our django project, we need to add a line in our global bikebackend urls.py file (don't confuse with the previous urls.py that we added in the api directory). Just add the line:

path('api/', include('api.urls')),

The full file should be:

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from django.conf import settings
from django.conf.urls.static import static
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
urlpatterns = [
    path('', include(router.urls)),
    path('jet/', include('jet.urls', 'jet')),  # Django JET URLS
    path('admin/', admin.site.urls),
    path('api/', include('api.urls')),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

By adding this /api route, all endpoints will be callable with the url : https://ourdomain.com/api

Of course at this time, our endpoints list is empty, so now we will create our first endpoint.