Home > Software design >  How to remove last / character in root URL API in Django
How to remove last / character in root URL API in Django

Time:11-18

API only work with url below:

http://127.0.0.1:1997/api/v1/groups/

How can I remove the last '/' so that it works like below:

http://127.0.0.1:1997/api/v1/groups

My config url code:

import os

from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from rest_framework.schemas import get_schema_view
from rest_framework_swagger.renderers import SwaggerUIRenderer, OpenAPIRenderer
from groups import views as group_views

API_VERSION = os.getenv('API_VERSION')
API_ROOT = f"api/{API_VERSION}/"

router = routers.DefaultRouter()
router.register('groups', group_views.GroupViewSet)

schema_view = get_schema_view(
  title='next_blog',
  renderer_classes=[OpenAPIRenderer, SwaggerUIRenderer])
urlpatterns = [
  path('admin', admin.site.urls),
  path('api_auth', include(
    'rest_framework.urls', namespace='rest_framework')),
  path('docs', schema_view, name='docs'),
  path(API_ROOT, include(router.urls)),
]

Thanks everyone !

CodePudding user response:

Set the trailing_slash argument to False when instantiating the router.

router = DefaultRouter(trailing_slash=False)

Django Rest Framework Default Router

  • Related