简体   繁体   English

Django Rest Framework一次不会显示多个URL?

[英]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.

相关问题 在Django REST Framework中通过多个参数来反向URL - Reverse URL by more than one parameter in Django REST Framework Django Rest 框架上 url 中的多个值 - More than one value in url on Django Rest Framework 如何在django rest框架中将多个变量传递给modelViewSet? - How to pass more than one variables to modelViewSet in django rest framework? get() 返回了多个产品 -- 它返回了 2 Django Rest 框架 - get() returned more than one Product -- it returned 2 Django Rest Framework Django rest framework get()返回了多个对象,尽管使用了filter()方法 - django rest framework get() returned more than one object though filter() method is used django rest_framework 在创建 object 时说 get() 返回了多个请求——它返回了 2 - django rest_framework when creating object says get() returned more than one Request -- it returned 2 django rest_framework 错误 MultipleObjectsReturned get 方法返回多个 - django rest_framework error MultipleObjectsReturned get method return more than one Django Rest Framework 不会让我拥有多个权限 - Django Rest Framework won't let me have more than one permission 在django-rest-framework-jwt中存储超过默认信息 - Store more than default information in django-rest-framework-jwt get() 返回了多个模块——它返回了 2 个! (休息 API - Django) - get() returned more than one Modules -- it returned 2! (REST API - Django)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM