Problem when displaying routes in Root Api

Asked

Viewed 74 times

2

I have a file urls.py in the project with my API routes, I am importing the routes and concatenating into a variable called api_urls, to then include the variable with the routes, but when doing this, only the router_user routes appear in the Root API page, because, in order were the first that I added in api_urls. How do I list all the routes on the Root API page?

Urls of the Project

from django.contrib import admin
from django.urls import path, include
from django.conf import settings

from usuarios.urls import router_usuario
from avisos.urls import router_avisos
from boletos.urls import router_boletos
from condominio.urls import router_condominio
from endereco.urls import router_endereco
from registros.urls import router_registros
from reservas.urls import router_reservas
from taxas.urls import router_taxas

api_urls = router_usuario.urls + router_avisos.urls + 
router_boletos.urls + router_condominio.urls \
+ router_endereco.urls + router_registros.urls + router_reservas.urls 
+ router_taxas.urls

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('rest_framework.urls')),
    path('', include(api_urls)),
]

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns

2 answers

3

Create a list, add the urls to that list and after that you will have all the urls

from django.contrib import admin
from django.urls import path, include
from django.conf import settings

from usuarios.urls import router_usuario
from avisos.urls import router_avisos
from boletos.urls import router_boletos
from condominio.urls import router_condominio
from endereco.urls import router_endereco
from registros.urls import router_registros
from reservas.urls import router_reservas
from taxas.urls import router_taxas

lista_urls = [router_usuario.urls, router_avisos.urls, 
router_boletos.urls, router_condominio.urls,
router_endereco.urls, router_registros.urls, router_reservas.urls, router_taxas.urls]

api_urls = [row for row in lista_urls]


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('rest_framework.urls')),
    path('', include(api_urls)),
]

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns
  • Unfortunately, it did not work. An error has occurred, please follow stacktrace: https://pastebin.com/XwEPbvfL

  • Pastebin is blocked for me to see here, if you can put in another location: dontpad

  • there you are: http://dontpad.com/XwEPbvfL

  • How were the urls coming the way they were before? Print api_urls and send it to me on the same site please

  • These are the urls I’m importing to the.py urls of the project: http://dontpad.com/urls

  • Previously, urls were being added like this: http://dontpad.com/api_urls, as shown in the question, however, only the first url I add in api_urls is shown in Root Api

Show 1 more comment

1


You’re right when you say only the first urls of your + appear on the site. This is because you are using the DefaultRouter in all apps and then doing the import and the new list from the .urls.

Following the line of using the DefaultRouter, the correct way of doing the "merge" between them is:

router = routers.DefaultRouter()
router.register("rota-1", Rota1ViewSet)

router2 = routers.DefaultRouter()
router2.register("rota-2", Rota2ViewSet)


router3 = routers.DefaultRouter()
router3.registry.extend(router.registry)
router3.registry.extend(router2.registry)

urlpatterns = [
    path("", include(router3.urls)),
    ...
]

In your case, it would be something like this:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings

from usuarios.urls import router_usuario
from avisos.urls import router_avisos
from boletos.urls import router_boletos
from condominio.urls import router_condominio
from endereco.urls import router_endereco
from registros.urls import router_registros
from reservas.urls import router_reservas
from taxas.urls import router_taxas
from rest_framework import routers

router = routers.DefaultRouter()
router.registry.extend(router_usuario.registry)
router.registry.extend(router_avisos.registry)
router.registry.extend(router_boletos.registry)
router.registry.extend(router_condominio.registry)
router.registry.extend(router_endereco.registry)
router.registry.extend(router_registros.registry)
router.registry.extend(router_reservas.registry)
router.registry.extend(router_taxas.registry)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('rest_framework.urls')),
    path('', include(router.urls)),
]

If I haven’t forgotten any import or something like that should work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.