How can I use multiple routers for a Django Rest Framework project?
I am facing a hard time on trying to use a router for an app and another for another app.
# api/urls.py
from django.urls import path, include
from .views import UserViewSet, DepartmentViewSet, UserLoginView, UserTokenRefreshView
router = DefaultRouter()
router.get_api_root_view().cls.__name__ = "E.R.P. API"
router.get_api_root_view().cls.__doc__ = "API do ERP"
router.register('users', UserViewSet, basename='user')
router.register('departments', DepartmentViewSet, basename='department')
app_name = 'api'
urlpatterns = [
path('login/', UserLoginView.as_view(), name='login'),
path('token/refresh/', UserTokenRefreshView.as_view(), name='token_refresh'),
path('', include(router.urls)),
path('history/', HistoryView.as_view(), name='history'),
]
# mobile_app/urls.py
from django.urls import path, include
from .views import CustomerLoginView
mobile_app_router = DefaultRouter()
mobile_app_router.get_api_root_view().cls.__name__ = "Mobile App API"
mobile_app_router.get_api_root_view().cls.__doc__ = "API do Aplicativo Mobile"
app_name = 'mobile_app'
urlpatterns = [
path('login/', CustomerLoginView.as_view(), name='login'),
path('', include(mobile_app_router.urls))
]
# erp/urls.py
from django.contrib import admin
from django.urls import path, re_path, include
from . import settings
from django.conf.urls.static import static
from notifications import urls as notifications_urls
urlpatterns = [
path('api/', include('api.urls'), name='api'),
path('api/m/', include('mobile_app.urls'), name='api_mobile'),
path('', admin.site.urls),
re_path(r'^inbox/notifications/', include(notifications_urls, namespace='notifications')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The name and doc are being mixed, if I try to use a swagger or redoc (drf_yasg
) the URLs are mixed. It is like they are the same. How can I do it correctly?
I tried to use multiple routers for a Django Rest Framework and I expect use it as different routers in different urls.