Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 18, 2021 05:45 pm GMT

Document and Test Django APIs with Swagger (Part 1)

Building APIs with Django Rest Framework is one of the Best Technology to work with, you have so much ways to build an API. But the problem arises when your APIs are not properly documented. But we have Swagger.

INDEX

  • Introduction
  • Setting the Environment
  • Configuring Django
  • Creating Models and Serializers
  • Building APIs
  • Building Documentation
  • Testing our APIs

Introduction

Swagger is one of the widely used API documentation and Testing tool. In this series we are going to use drf-yasg2 as a Wrapper for our Django / DRF Project.

Following is a Snapshot of API Documentation.
Alt Text

Setting the Environment

Start by creating a fresh Django Project. For the shake of learning we are going to create APIs for a TODO Application, In which users can Create, Read, Update and Delete (CRUD) TODOs.

Create Python Virtual Environment

$ python3 -m venv env

Activate the Virtual Environment

$ source ./env/bin/activate

Now, install modules

$ pip install django djangorestframework drf-yasg2

Create Django project myapp in present working directory

$ django-admin startproject myapp .

Create Django app todos

$ django-admin startapp todos

Register todos and drf_yasg2 in djangos settings.py (myapp/settings.py)

INSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'rest_framework',    'drf_yasg2',    'todos',]

Also, add the following code block in the same file (settings.py)

REST_FRAMEWORK = {    'DEFAULT_PERMISSION_CLASSES': (        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',    )}SWAGGER_SETTINGS = {   'SECURITY_DEFINITIONS': {      'Bearer': {            'type': 'apiKey',            'name': 'Authorization',            'in': 'header'      }   }}

Edit the projects urls.py file (myapp/urls.py)

from django.contrib import adminfrom django.urls import path, re_pathfrom drf_yasg2.views import get_schema_viewfrom drf_yasg2 import openapischema_view = get_schema_view(    openapi.Info(        title="MyApp Docs.",        default_version='v1',    ),    public=True,)urlpatterns = [    path('admin/', admin.site.urls),    re_path(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),]

Run Django Migrations

$ python manage.py migrateApplying contenttypes.0001_initial... OKApplying auth.0001_initial... OKApplying admin.0001_initial... OKApplying admin.0002_logentry_remove_auto_add... OKApplying admin.0003_logentry_add_action_flag_choices... OKApplying contenttypes.0002_remove_content_type_name... OKApplying auth.0002_alter_permission_name_max_length... OKApplying auth.0003_alter_user_email_max_length... OKApplying auth.0004_alter_user_username_opts... OKApplying auth.0005_alter_user_last_login_null... OKApplying auth.0006_require_contenttypes_0002... OKApplying auth.0007_alter_validators_add_error_messages... OKApplying auth.0008_alter_user_username_max_length... OKApplying auth.0009_alter_user_last_name_max_length... OKApplying auth.0010_alter_group_name_max_length... OKApplying auth.0011_update_proxy_permissions... OKApplying auth.0012_alter_user_first_name_max_length... OKApplying sessions.0001_initial... OK

Run Django Development Server

$ python manage.py runserver

Visit http://127.0.0.1:8000/swagger/ You should see blank API Documentation/Testing Web Page

Alt Text

Congratulations! You have Successfully generated API Documentation / Testing Page

Whats Next?

In the Next Tutorial, Well Create Models, Serializers and Views for our TODO Application.

Thank you
With Team Stackless Tech


Original Link: https://dev.to/rajeshj3/document-and-test-django-apis-with-swagger-part-1-2le7

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To