[英]Django Rest Framework will not display more than one URL at time?
I have a project with multiple apps. 我有一个包含多个应用的项目。 Each app has a urls.py.
每个应用都有一个urls.py. I point to each of those from the project urls.py.
我指向项目urls.py中的每一个。 All Urls are accessible however, they don't all display in DRF.
但是,所有URL都可以访问,它们并不都显示在DRF中。
Here is the code: 这是代码:
from django.conf.urls import url, include
from rest_framework import routers
from employees.urls import employees_router
from access.urls import access_router
router = routers.DefaultRouter()
#API ENDPOINTS
urlpatterns = [
url(r'^api/', include(employees_router.urls)),
url(r'^api/', include(access_router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
The problem is ONLY the first URL listed will display - in this case employees. 问题是只显示列出的第一个URL - 在这种情况下是员工。 If I comment that one out, then the access_router urls display (users and groups) as separate links.
如果我对其中的一个进行评论,则access_router URL会将(用户和组)显示为单独的链接。 Why will ALL of the URLS for the rest_framework notdisplay at the same time - in a list format?
为什么rest_framework的所有URL都不能同时显示 - 以列表格式显示?
Django starts at the top of the url patterns, and stops as soon as it finds a match. Django从url模式的顶部开始,并在找到匹配后立即停止。 Since you have two urls using the same regex
'^api/'
, the second include will never be used. 由于您有两个使用相同正则表达式
'^api/'
URL,因此永远不会使用第二个包含。
Alternatively, you don't need a router for each app. 或者,您不需要为每个应用程序使用路由器。 You can register multiple viewsets with the same router:
您可以使用相同的路由器注册多个视图集:
from access.urls import AccessViewSet
from employees.urls import EmployeesViewSet
router = routers.DefaultRouter()
router.register(r'access', AccessViewSet)
router.register(r'employees', EmployeesViewSet)
Then include the default router in your url patterns: 然后在您的网址格式中包含默认路由器:
urlpatterns = [
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.