Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I point to each of those from the project urls.py. All Urls are accessible however, they don't all display in 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. If I comment that one out, then the access_router urls display (users and groups) as separate links. Why will ALL of the URLS for the rest_framework notdisplay at the same time - in a list format?

like image 916
Nicoale Avatar asked Oct 14 '15 13:10

Nicoale


1 Answers

Django starts at the top of the url patterns, and stops as soon as it finds a match. Since you have two urls using the same regex '^api/', the second include will never be used.

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'))
]
like image 159
Alasdair Avatar answered Oct 07 '22 17:10

Alasdair