TIL: Integrate Wagtail API into Local Namespace
,I wanted to incorporate a wagtail pages endpoint into django-cast. Although I successfully added it to the global URLConf as per the official documentation, django-cast serves as a third-party library, and its API should likely exist within the cast namespace. This didn't work and I ended up always seeing an error like this:
NoReverseMatch at /cast/api/pages/
'wagtailapi' is not a registered namespace
After a significant amount of experimentation, I eventually discovered a method to successfully employ the WagtailAPIRouter
in the local URLConf:
# api views.py
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.api.v2.views import PagesAPIViewSet
wagtail_api_router = WagtailAPIRouter("cast:api:wagtail")
wagtail_api_router.register_endpoint("pages", PagesAPIViewSet)
# local urls.py
app_name = "api"
urlpatterns = [
...,
# wagtail api
path("wagtail/", include((views.wagtail_api_router.get_urlpatterns(), "api"), namespace="wagtail")),
]
Here's how the pages endpoint reversal works now:
reverse("cast:api:wagtail:pages:listing")