实例化路由器时,将trailing_slash参数设置为False。
API仅适用于以下url:
http://127.0.0.1:1997/api/v1/groups/
我如何删除最后一个"/",使其工作方式如下:
http://127.0.0.1:1997/api/v1/groups
我的配置url代码:
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)),
]
谢谢大家!
router = DefaultRouter(trailing_slash=False)
Django Rest Framework默认路由器