Easy steps to mastering build API with Django REST Framework

Youvandra Febrial
Nov 3 · 3 min read

What is REST API?
REST (Representational State Transfer) is a web-based communication architecture standard that is often applied in the development of web-based services. Generally use HTTP (Hypertext Transfer Protocol) as a protocol for data communication.

The REST architecture, which is generally run via HTTP (Hypertext Transfer Protocol), involves the process of reading certain web pages that contain an XML or JSON file. This file describes and contains the content to be presented. After going through a certain definition process, consumers will be able to access the intended application interface.

Preparation

  1. Install Python version (2.7 , 3.2 , 3.3 , 3.4 , 3.5)
sudo apt-get update

sudo apt-get install python3.5

2. Install Django 2.2

3. Install Django Rest Framework

pip install djangorestframework
pip install markdown # Markdown support for the browsable API.
pip install django-filter # Filtering support

after preparation is complete, the next step is to set the virtual environment :

set the virtual environment is complete ?

let’s begin

1 . make a django project first through the terminal :

django-admin.py startproject samplenote

in terminal :

$ cd samplenote

2. After the project has been created, continue with the app:

django-admin.py startapp api

your app is created?

3. after the app has been created, in setting.py add the app rest framework like this:

setting.py

INSTALLED_APPS = (
...
'rest_framework',

'notes',
)

4. make a system model to be made

models.py

from django.db import models
from django.utils import timezone
from django.core.validators import MaxValueValidator, MinValueValidator
# Create your models here.

class Note(models.Model):

title = models.CharField(max_length=100)
description = models.CharField(max_length=400)
created_at = models.DateTimeField(default=timezone.now)
created_by = models.CharField(max_length=50, blank=True, null=True)
priority = models.IntegerField(validators=[MinValueValidator(1),
MaxValueValidator(5)])

then makemigrations and migrate in the terminal :

$ python manage.py makemigrations$ python manage.py migrate 

4. After the model is made, the serializers are made, this is an important part of making the API endpoint

create new file note_api.py in folder note

note_api.py

from rest_framework import serializers
from .models import Note
++++++
class NoteSerialiser(serializers.HyperlinkedModelSerializer):

class Meta:
model = Note
fields = ('id', 'title', 'description', 'created_at', 'created_by', 'priority')
+++

The serializer functions to manage what data we will release through the API endpoint, for example in the Song model we have album, file_type, and song_title fields. well, we can make this API output only output album fields and song_title, so without the file_type field.

5. Next is to create a View to display the results of serializers

+++ from rest_framework import viewsets+++ class NoteViewSet(viewsets.ModelViewSet):
queryset = Note.objects.all()
serializer_class = NoteSerialiser

6. Create url for Endpoint API

samplenote/urls.py

from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from note.note_api import NoteViewSet

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'notes', NoteViewSet)

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api/', include(router.urls)),
]

in terminal :

$ python manage.py runserver

if you look in the terminal like this :

System check identified no issues (0 silenced).
— — — — — — — — — — — — — — —
Django version 1.9, using settings ‘samplenote.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

write in seacrh engine like chrome or mozzila, 127.0.0.1:8000/api/

RESULT :

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade