Build a Rest API with Python using Django - The easiest way

python Apr 23, 2020

In this tutorial we will transform our Django phonebook Application to a Rest API

A REST API defines a set of functions which developers can perform requests and receive responses via HTTP protocol such as GET, POST, PUT and DELETE

Think REST API as a web service that provide you the data you want to use in your application(mobile or front-end client).

The key component for a REST API request are:

GET — The most common option, returns some data from the API based on the given endpoint .

POST — Creates a new record and add it to the database.

PUT — Update an existing record.

DELETE — Deletes the record on the given endpoint.

Getting started

$ git clone https://github.com/xarala221/django-phonebook.git
$ cd django-phonebook

Application setup

$ pipenv install
Creating a virtualenv for this project…
Using /usr/bin/python3.7m (3.7.5) to create virtualenv…
$ pipenv shell
Spawning environment shell (/usr/bin/zsh). Use 'exit' to leave.
. /home/username/.local/share/virtualenvs/phonebook_rest_api-9zIZds3o/bin/activate

Run the application

(my-env) $ python manage.py runserver

Notice i use pipenv instead of pip but you can use pip if you want it's up to you.

The application is running at http://localhost:800/

.
├── accounts
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── contact
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── manage.py
├── phonebook
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── Pipfile
├── Pipfile.lock
├── README.md
├── requirements.txt
└── templates
   ├── accounts
   │   ├── login.html
   │   └── register.html
   ├── base.html
   ├── contact
   │   ├── contact_details.html
   │   ├── contact_list.html
   │   ├── delete_contact.html
   │   ├── new_contact.html
   │   └── update_contact.html
   ├── index.html
   └── partials
       └── _navbar.html

This is our folder structure.

Setup Django REST Framework

Django REST framework is a powerful and flexible toolkit for building Web APIs.

(myenv) $ pipenv install djangorestframework

In phonebook/settings.py add

# Application definition
INSTALLED_APPS = [
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'rest_framework', # add this
   'contact',
]

Serialize the contact Table(Model)

Let's create a new file -- contact/serializers.py

# contact/serializers.py
from rest_framework import serializers
from .models import Contact


class ContactSerializer(serializers.ModelSerializer):
   class Meta:
       model = Contact
       fields = '__all__'

Preparing the data

Create a new file -- contact/api.py

# contact/api.py
from rest_framework import viewsets
from .serializers import ContactSerializer
from .models import Contact


class ContactViewSet(viewsets.ModelViewSet):
   serializer_class = ContactSerializer
   queryset = Contact.objects.all()

Update our contact/urls.py

from django.urls import path, include # add this
from rest_framework.routers import DefaultRouter  # add this
from .views import (
   index, contact_list,
   new_contact, contact_details,
   update_contact, delete_contact
)
from .api import ContactViewSet  # add this
router = DefaultRouter()  # add this
router.register(r'contacts', ContactViewSet,
               basename='contact')  # add this


urlpatterns = [
   path("api/", include(router.urls)),
   path("", index, name="home"),
   path("contacts/", contact_list, name="contacts"),
   path("contacts/new/", new_contact, name="new"),
   path("contacts/<int:id>/details/", contact_details, name="details"),
   path("contacts/<int:id>/update/", update_contact, name="update"),
   path("contacts/<int:id>/delete/", delete_contact, name="delete"),
]

Open your browser and go to http://localhost:8000/api/
You will see something like this :

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
   "contacts": "http://localhost:8000/api/contacts/"
}

Click on the link what you see ?

We started sharing these tutorials and articles to help and inspire developers and engineers around the world. If our blog has been helpful to you, feel free to buy us a coffee to keep us going :).

Buy us a coffeeBuy us a coffee

Ousseynou Diop

Full stack Developer & Python Lover

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.