Django API Documentation with ReDoc & drf-yasg

Last updated 4 months ago | 358 views 75     5

Tags:- Python Django DRF

Introduction: Why Use ReDoc for Django REST API Documentation?

Great API documentation is non-negotiable. Whether you're building public APIs or internal microservices, good docs:

  • Reduce bugs and misunderstandings

  • Help frontend/mobile teams integrate faster

  • Simplify onboarding for new developers

  • Improve API discoverability

While Swagger UI is popular, ReDoc stands out for its clean, responsive, and three-panel layout that’s ideal for large APIs.

In this guide, you'll learn how to create beautiful, real-time updating ReDoc documentation for your Django REST Framework project using drf-yasg.


⚙️ Setting Up ReDoc in Django

✅ Step 1: Install drf-yasg

First, you need drf-yasg, which supports both Swagger and ReDoc out of the box.

pip install drf-yasg

✅ Step 2: Add to INSTALLED_APPS

In your settings.py, register it:

INSTALLED_APPS = [
    ...
    'rest_framework',
    'drf_yasg',
]

✅ Step 3: Configure Schema View with ReDoc Support

Update your urls.py to include schema generation with ReDoc:

from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from django.urls import path, re_path

schema_view = get_schema_view(
   openapi.Info(
      title="My Django API",
      default_version='v1',
      description="Interactive API documentation powered by ReDoc",
      terms_of_service="https://www.example.com/terms/",
      contact=openapi.Contact(email="[email protected]"),
      license=openapi.License(name="BSD License"),
   ),
   public=True,
   permission_classes=[permissions.AllowAny],
)

urlpatterns = [
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]

✅ Step 4: Launch and Visit the Docs

Run your server:

python manage.py runserver

Navigate to:

http://127.0.0.1:8000/redoc/

You’ll see a full-featured ReDoc interface with:

  • Endpoint listing

  • Schema details

  • Query params

  • Request/response examples


Full Working Example

Here’s a minimal Django setup that returns a welcome message and documents it with ReDoc.

views.py

from rest_framework.decorators import api_view
from rest_framework.response import Response
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

@swagger_auto_schema(
    method='get',
    operation_description="Returns a welcome message.",
    responses={200: openapi.Response("Success", examples={"application/json": {"message": "Welcome to the API!"}})}
)
@api_view(['GET'])
def welcome_view(request):
    return Response({"message": "Welcome to the API!"})

urls.py (app-level)

from django.urls import path
from .views import welcome_view

urlpatterns = [
    path('welcome/', welcome_view, name='welcome'),
]

urls.py (project-level)

from django.contrib import admin
from django.urls import path, include, re_path
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
from rest_framework import permissions

schema_view = get_schema_view(
   openapi.Info(
      title="Demo API",
      default_version='v1',
      description="Django API with ReDoc documentation",
   ),
   public=True,
   permission_classes=[permissions.AllowAny],
)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('myapp.urls')),
    path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
]

Tips & Common Pitfalls

✅ Best Practices

  • Use swagger_auto_schema for every endpoint to define expected inputs and outputs.

  • Document query parameters using openapi.Parameter.

  • Keep documentation synced with code to avoid outdated API references.

❌ Common Mistakes to Avoid

Problem Fix
ReDoc page is blank Make sure schema view is correctly configured and view paths exist
403 on schema route Set permission_classes=[AllowAny] in get_schema_view()
Endpoints not showing Check that all views are registered properly in urlpatterns
Missing parameter documentation Use swagger_auto_schema to explicitly define query or body params

Swagger UI vs ReDoc: Quick Comparison

Feature Swagger UI ReDoc
UI Style Interactive & expandable Clean, column-based layout
Customization High Medium
Navigation Tree-based Scrollable with index
Ideal for Quick testing Large APIs & production docs
Built-in with DRF? No No

You can include both Swagger and ReDoc in your app and offer users a choice.


✅ Conclusion: Make Your APIs Speak for Themselves

Using ReDoc with Django REST Framework gives you clean, modern, and professional-grade API documentation with minimal setup. It helps:

  • Streamline frontend/backend collaboration

  • Reduce guesswork for API consumers

  • Improve developer confidence and productivity


Final Takeaways

  • Use drf-yasg to generate OpenAPI schemas for DRF.

  • Enable ReDoc UI for clean, scrollable documentation.

  • Always annotate your views for clarity and accuracy.

  • Keep your API documentation versioned and publicly accessible when appropriate.