How to Document API Requests using Django Rest Swagger

Reading Time : ~ .

In Developing an application, we'll write our required API's. To Document these API's we use Django Rest Swagger.

Django Rest Swagger is used to Document your API for eg., Listing all Your project Rest API's in the browser with brief description abou the API.

It is Open Source so you can contribute to the project.

If your API's available to the users with description, so UI developers can understand and test your API's and use it accordingly.

First we need to install the swagger.

pip install django-rest-swagger

Add "rest_framework_swagger" to Django settings INSTALLED_APPS

INSTALLED_APPS = (

    ...

    'rest_framework_swagger',

)

Include the rest_framework_swagger URLs to a path of your choice

patterns = ('',

    ...

    url(r'^docs/$', schema_view, name="schema_view"),

)

In your API Views

from rest_framework.decorators import api_view, permission_classes, renderer_classes

from rest_framework.permissions import AllowAny

from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer

@api_view()

@permission_classes((AllowAny, ))

@renderer_classes([OpenAPIRenderer, SwaggerUIRenderer])

def schema_view(request):

    generator = schemas.SchemaGenerator(title='Rest Swagger')

    return Response(generator.get_schema(request=request))


If you are not authorized, This document List all the API which doesnt reguire any permission to get authorize.

If you want to authorize user you need to add these swagger settings in your settings.py file. By this you will be provide with Authorize Button in the header when user hits the swagger document URL, When you click on it you will be prompt with a pop-up to enter details of Api key authorization. By providing your API Key the user gets Authorize.

SWAGGER_SETTINGS = {

    'USE_SESSION_AUTH': False,

    'api_version': '0.1',

    'enabled_methods': [

        'get',

        'post',

    ],

    'SECURITY_DEFINITIONS': {

        "api_key": {

            "type": "apiKey",

            "name": "Authorization",

            "in": "header"

          },

    },

}
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Understanding Routers in Django-Rest-Framework Anjaneyulu Batta

By using routers in django-rest-framework we can avoid writing of url patterns for different views. Routers will save a lot of time for developing the ...

Continue Reading...
Sending emails using sendgrid on heroku for a Django App Vinisha Naladala

Integrate Sendgrid API to your Heroku app to deliver simplified emails like any notification emails, user signups etc.

Continue Reading...
Inclusion Tags Ashwin Kumar

Django’s template system comes with a wide variety of built-in tags and filters designed to address the presentation logic needs of your application. You can ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties