Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate django-rest-swagger 2.0 with existing DRF application(Beginner)

I am trying to integrate swagger 2.0 with existing DRF application but it gives following error in browser after runningpython manage.py runserver:

TemplateDoesNotExist at /swagger rest_framework_swagger/index.html Request Method: GET Request URL: http://127.0.0.1:8000/swagger Django Version: 1.10 Exception Type: TemplateDoesNotExist Exception Value: rest_framework_swagger/index.html Exception Location: C:\Users\MHAZIQ~1\Desktop\Tkxel\mmg-git\venv\lib\site-packages\django\template\loader.py in get_template, line 25

I have added following lines in views.py:

from rest_framework_swagger.views import get_swagger_view

schema_view = get_swagger_view(title='Pastebin API')

And I have added following lines in urls.py:

url(r'^swagger', views.schema_view),

I ve tried applying following solution: TemplateDoesNotExist at /docs/ rest_framework_swagger/index.html

but it didnot solve my problem, Can anyone please help me in this regard?

like image 241
Haziq Avatar asked Mar 10 '23 08:03

Haziq


2 Answers

Add 'rest_framework_swagger' to INSTALLED_APPS in Django settings.

settings.py

  INSTALLED_APPS = [
    ...
        'rest_framework_swagger',
    ]

https://django-rest-swagger.readthedocs.io/en/latest/

like image 196
akpp Avatar answered Mar 11 '23 21:03

akpp


After several hours of research I have found the problem with my code, as I was adding swagger into an existing project, it didnt have following parameters in settings.py:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},]

Now swagger works absolutely fine!

like image 22
Haziq Avatar answered Mar 11 '23 20:03

Haziq