Easy steps to mastering build API with Django REST Framework
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
- Install Python version (2.7 , 3.2 , 3.3 , 3.4 , 3.5)
sudo apt-get update
sudo apt-get install python3.52. 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 supportafter 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 samplenotein terminal :
$ cd samplenote2. After the project has been created, continue with the app:
django-admin.py startapp apiyour 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 runserverif 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 :


