url优化
- 优化前
# urls.py
from django.urls import path
from projects import views
urlpatterns = [
path('projects/', views.ProjectViewSet.as_view({
'get': 'list',
'post': 'create'
}), name='projects-list'),
path('projects/<int:id>/', views.ProjectViewSet.as_view({
'get': 'retrieve',
'put': 'update',
'delete': 'destroy'
}))
]
- 优化后
# urls.py
from rest_framework import routers
from projects import views
router = routers.DefaultRouter()
router.register(r'projects', views.ProjectViewSet)
urlpatterns = []
urlpatterns += router.urls