Before proceeding to next, please go through these 2 links to understand about REST API , Server & preparing the Environment for development.

REST API Basic

Environment Preparation / Setup

If you have Ubuntu environment ready please follow the next steps. This tutorial is about for Intermediate+ level.

Go to Desktop in your ubuntu > Create a folder named “tutorial_project” > Open Terminal window on this folder (tutorial_project)

# Create the project directory 
sudo mkdir tutorial_project

cd tutorial_project

# Create a virtual environment to isolate our package dependencies locally
sudo python3 -m venv env

source env/bin/activate  # On Windows use `env\Scripts\activate`

# Install Django and Django REST framework into the virtual environment
sudo pip install django

sudo pip install djangorestframework

# Set up a new project with a single application
django-admin startproject tutorial .  # Note the trailing '.' character

cd tutorial

django-admin startapp quickstart

cd ..

# Change the folder's permission
sudo chmod -R 777 ./


Open project with PyCharm. Go to settings.py > add ‘rest_framework’, on INSTALLED_APPS

Replace the default database code in the settings.py file.

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'rest-tutorial',
    }
}

Add the code below at the bottom of the settings.py file.

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10,
}

Install MongoDB by following this tutorial.

Install djongo (communicator between Python and MongoDB)

sudo pip install djongo

sudo python3 manage.py migrate

Now run the server:

sudo python3 manage.py runserver

If everything is ok, you will see that server started successfully.

No go to quickstart folder, select views.py file and replace the code below:

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

import json


class WelcomeViewSet(APIView):
    def get(self, request, format=None):
        # Show a welcome greetings
        retJson = {
            "status": "ok",
            "msg": "Welcome Turzo! Your Django & MongoDB based API server is working successfully!"
        }

        return Response(retJson)


In tutorial folder, select urls.py file and replace the code below:

from django.urls import include, path
from rest_framework import routers
from alert.alertapp import views


# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [

    path('api/welcome', views.WelcomeViewSet.as_view()),
    
]

Now run the server again.

sudo python3 manage.py runserver

Open Postman or Browser, test the api

localhost:8000/api/welcome

If you see the response like below then your API server based on Python Django framework is ready to rock!

Response:

{
“status”: “ok”,
“msg”: “Welcome Turzo! Your Django & MongoDB based API server is working successfully!”
}

In next tutorial, we will discuss about JWT. We will implement tokenized API to secure our APIs.

Leave a Reply

Your email address will not be published. Required fields are marked *