Swagger Integration with Python Django
Last Updated :
11 May, 2024
Integrating Swagger with Django REST Framework can be quite useful for documenting and testing your API. One popular tool for integrating Swagger with Django REST Framework is drf-yasg (Yet Another Swagger Generator). In this article, we will see how to integrate Swagger with the Django REST framework.
Swagger Integration With Python Django
Below is the step-by-step procedure by which we can integrate swagger with the Django REST framework using Python:
Starting the Project Folder
To start the project use this command
django-admin startproject core
cd core
To start the app use this command
python manage.py startapp home
Now add this app to the ‘settings.py’
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
'rest_framework',
'drf_yasg',
]
install restframework and drf_yasg
pip install drf-yasg
pip install djangorestframework
File Structure

core/settings.py: This code configures Swagger settings for Django REST Framework API, specifying a Bearer token security definition and disabling session-based authentication. Add below code in settings.py for swagger integration:
Python
SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
'Bearer': {
'type': 'apiKey',
'name': 'Authorization',
'in': 'header'
}
},
'USE_SESSION_AUTH': False,
}
home/swagger.py: Create swagger.py file and add the below code. This code sets up a Swagger schema view for the Django REST Framework API, defining metadata such as title, version, description, terms of service, contact, and license. It allows public access and permits any user to view the schema.
Python
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="My API",
default_version='v1',
description="Test description",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="[email protected]"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
home/urls.py: This Django code imports the admin module for administrative tasks, defines URL patterns for the admin interface and Swagger documentation. The schema_view is imported from a module named swagger, and it's used to render the Swagger UI for API documentation at the '/swagger/' endpoint with zero caching.
Python
from django.contrib import admin
from django.urls import path
from home.swagger import schema_view
urlpatterns = [
path('admin/', admin.site.urls),
path('swagger/', schema_view.with_ui('swagger',
cache_timeout=0), name='schema-swagger-ui'),
]
Deployment of the Project
Run these commands to apply the migrations:
python3 manage.py makemigrations
python3 manage.py migrate
Run the server with the help of following command:
python3 manage.py runserver
Output
Swagger Integration With Django
Similar Reads
Integrating TinyMCE with Django
TinyMCE is a online rich text editor which is fully flexible and provides customisation. mostly used to get dynamic data such as articles in GFG and much more, their is no static database for posts Installation - To integrate it with Django web app or website you need to first install its pip librar
2 min read
Python Django Queryset Filtering
Django has an inbuilt function for filtering the dataset. But it is lacking in the exclusion of dataset. So for this, we have other 2 solutions to filter out data which not equal to the mentioned dataset. Here, We are going to understand and learn through a mini Django project using Python. Let's cr
4 min read
Integrate the QuickBooks API with the Python Django
QuickBooks is a powerful accounting software that helps businesses manage their finances efficiently. Integrating the QuickBooks API with a Django application can automate many financial processes, saving time and reducing errors. This article will guide you through the steps required to integrate t
6 min read
Views In Django | Python
Django Views are one of the vital participants of the MVT Structure of Django. As per Django Documentation, A view function is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, a redirect, a 404 error, an XML document, an ima
6 min read
Python Swagger Annotations for API Documentation
Your API documentation and specification capabilities can be substantially improved by using Python's FastAPI with Swagger annotations. This comprehensive tutorial covers all the bases, from introduction to execution. In the context of Python, Swagger annotations are often used in combination with f
4 min read
Getting started with Django
Python Django is a web framework that is used to create web applications very efficiently and quickly. Django is called a battery included framework because it contains a lot of in-built features such as Django Admin Interface, default database - SQLite3, etc. Django provides various ready-made comp
15+ min read
Razorpay Integration in Django
Payments are an integral part of the online ecosystem, be it at e-commerce stores to process orders or a simple donation to someone. Integrating a payment gateway into your website can be a tedious process. Let us have a look at how you can integrate Razorpay into a Django website. What are we build
6 min read
Django Google Calender Integration in Python
Google Calendar is a fantastic tool for managing your schedule. It's user-friendly and helps you keep track of all your important events and appointments. You can create events, set reminders, and receive notifications so you don't miss anything. In this article, we will see how we can integrate Goo
6 min read
Form Submission API with Swagger Editor and Python Flask
Creating user-friendly APIs is essential for seamless interaction between applications. Leveraging tools like Swagger Editor alongside Python Flask, developers can streamline the process of designing, documenting, and implementing APIs. In this guide, we'll explore how to craft a Form Submission API
3 min read
Learn to use Websockets with Django
Django Channels is an extension of the Django framework that allows for handling WebSockets, HTTP2, and other protocols. It integrates with Djangoâs existing ORM and works seamlessly alongside Django views. In this tutorial, we will create a small Django project that displays "GeeksforGeeks" on the
4 min read