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 Python Swagger with Continuous Integration
The Continuous Integration (CI) has become a standard practice. It enables teams to build, test, and deploy code changes rapidly and reliably. The Swagger on the other hand is a powerful framework for designing, building, and documenting APIs. Integrating Swagger with the CI pipelines can streamline
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
How to Send Email with Django
Django, a high-level Python web framework, provides built-in functionality to send emails effortlessly. Whether you're notifying users about account activations, sending password reset links, or dispatching newsletters, Djangoâs robust email handling system offers a straightforward way to manage ema
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
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
Python | Django Admin Interface
Prerequisites: Django Introduction and Installation Creating a ProjectThe Django Admin Interface is one of the most powerful features of the Django framework. It provides a ready-to-use interface for managing project data through models, allowing developers and site administrators to perform Create,
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
3 min read
How to use Tailwind CSS with Django ?
Tailwind CSS has gained immense popularity among developers for its utility-first approach to styling web applications. Django on the other hand is a robust and flexible web framework written in Python. Combining these two powerful tools can enhance your Django projects. In this article, we will exp
4 min read