'drf_yasg.generators: path component of api base URL http://localhost:8080/ is ignored; use FORCE_SCRIPT_NAME instead
I am using swagger (drf_yasg.generators) with Django and I get the following error message
Error message is
drf_yasg.generators: path component of api base URL http://localhost:8080/ is ignored; use FORCE_SCRIPT_NAME instead
Here is the swagger
class SchemaGenerator(OpenAPISchemaGenerator):
def get_schema(self, request=None, public=False):
schema = super(SchemaGenerator, self).get_schema(request, public)
schema.basePath = os.path.join(schema.basePath, "")
return schema
if env.bool("DJANGO_DEBUG"):
schema_view = get_schema_view(
openapi.Info(
title="API",
default_version="v1",
description="API endpoints",
),
url="http://localhost:8080/",
public=True,
permission_classes=(permissions.AllowAny,),
urlconf="config.urls",
generator_class=SchemaGenerator,
)
Here is the URL
urlpatterns = [
path(settings.ADMIN_URL, admin.site.urls),
path("api/", schema_view.with_ui("swagger", cache_timeout=0), name="swagger"),
path("algo1/", include("core_functions.algo1.urls")),
]
Can someone please help with the error?
Solution 1:[1]
It seems that you are not using the right configuration of url
in the schema_view
.
Please try with the regular Django url configuration deleting the url
field customization like the following:
from django.urls import re_path
from drf_yasg import openapi
from drf_yasg.views import get_schema_view
from rest_framework import permissions
schema_view = get_schema_view(
openapi.Info(
title='API',
default_version='v1',
description='API endpoints',
terms_of_service='',
contact=openapi.Contact(email='[email protected]'),
license=openapi.License(name="[Add License Name Here]"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)
swagger_urlpatterns = (
re_path(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
re_path(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
re_path(r'^redoc/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
)
and then add the following at the end of your urls.py
:
# ...
urlpatterns += swagger_urlpatterns
I also suggest to avoid to create a custom generator_class
in the schema_view
unless this is strongly needed
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dos |