Swagger Integration in Django
<p>I need to integrate Swagger in Django. So, can anyone discuss the steps to integrate Swagger in Django. I need the full description.</p>

<p>Thanks in advance.</p>

<p>If you're trying to use Swagger, then I'm sure you're building an API (REST). So answering to your wide range question about Swagger integration with Django, you can use Django Rest Framework + Swagger (which I recommend) or Swagger only. What you need to do in this case:</p>

<h2>Django Rest Framework + Swagger</h2>

<pre><code>pip install django djangorestframework django-rest-swagger
</code></pre>

<p>then in your <code>settings.py</code> just include:</p>

<pre><code>INSTALLED_APPS = [
    ...,
    'rest_framework',
    'rest_framework_swagger',
]
</code></pre>

<p>then in your <code>urls.py</code>:</p>

<pre><code>from rest_framework import routers

from yourapp.accounts.views import UserViewSet


router = routers.DefaultRouter()
router.register(r'users', UserViewSet)

urlpatterns = [
    url(r'^api/v1/', include(router.urls)),
    ...
]
</code></pre>

<p>in <code>views.py</code>:</p>

<pre><code>from rest_framework import viewsets


class UserViewSet(viewsets.ReadOnlyModelViewSet):
    """
    This viewset automatically provides `list` and `detail` actions.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer
</code></pre>

<p>and after entering home page <code>/</code>, you will get swagger interface with all endpoints rendered (registered in router as in the example above).</p>

<h2>Django + Swagger</h2>

<pre><code>pip install django django-rest-swagger
</code></pre>

<p>then in your <code>settings.py</code>:</p>

<pre><code>INSTALLED_APPS = [
    ...
    'rest_framework_swagger',
    ...
]
</code></pre>

<p>in <code>urls.py</code>:</p>

<pre><code>from django.conf.urls import url
from views import schema_view


urlpatterns = [
    url('/', schema_view),
    ...
]
</code></pre>

<p>and in your example <code>views.py</code>:</p>

<pre><code>from rest_framework.decorators import api_view, renderer_classes
from rest_framework import response, schemas
from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer


@api_view()
@renderer_classes([OpenAPIRenderer, SwaggerUIRenderer])
def schema_view(request):
    generator = schemas.SchemaGenerator(title='Bookings API')
    return response.Response(generator.get_schema(request=request))
</code></pre>

<p>Reference:</p>

<p><a href="https://django-rest-swagger.readthedocs.io/en/latest/" rel="nofollow">Django REST Swagger</a></p>

<p><a href="http://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/" rel="nofollow">Django REST Framework</a></p>

python django python-2.7 swagger swagger-ui