An Application Programming Interface (API) is a method by which two computers are can communicate with each other. Web APIs are the type of APIs used on the internet. APIsallow developers to separate concerns in software either as functional components or a service-oriented manner.

Django is a Python framework that allows for building web applications in a fast and efficient way. A lot of times, the developer or team who utilizes an API is different from the developer or team who built it.

Hence the need to provide a means of communicating API functionalities in a precise and clear manner to other developers. Django REST framework provides the capability to build RESTful (Representational State Transfer) kind of APIs with the Django framework.

Swagger UI allows us to build our documentation in the form of HTML pages so it can be viewed and read with ease. In this article, we shall go over creating an API for managing student records in a school and document the API we create.

Outline

  1. Creating our Django Project
  2. Making a Model and Serializer
  3. Views and URLs
  4. Generating the Schema
  5. Generating the Documentation

1. Creating our Django Project

Let us start the project we are going to document by creating installing Django and setting up an app. We shall start by creating a directory (folder) for our project and navigate into the new directory. let’s call the directory school-api

mkdir school-api && cd school-api

BashCopy

Let us create a virtual environment with virtualenvto isolate our project from any other project on our computer. We shall name the new virtual environment env.

virtualenv env

BashCopy

We shall activate the virtual environment as follows

source env/bin/activate

BashCopy

Should we need to deactivate the virtual environment, we shall simply use the deactivate command, deactivate.

Now, as we have activated our virtual environment, let’s proceed to install necessary packages and software including the Django package, and the django-rest-framework , the package that allows us to use the Django REST framework.

pip install Django django-rest-framework

BashCopy

now, we shall create our Django project

django-admin startproject schoolService

BashCopy

Let’s change directory into the new project directory

cd schoolService

BashCopy

then, we’d go to to create an app to host our APIs

django-admin startapp api

BashCopy

Now, we will add the rest_framework and api app to the list of INSTALLED_APP in the settings.py file in the project directory, the schoolService folder.

# schoolService/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'api',
]

#django #django rest api #swagger ui #ui

Creating Django REST API Documentation with Swagger UI
1.90 GEEK