I’m having trouble with the Wagtail API. I set up a FlexPage model with several fields and StreamField content, along with custom blocks for YouTube, images, section headings, and a two-column layout. In my API configuration, I registered endpoints for pages, images, and documents.
After publishing a new page, I expected it to appear at http://localhost:8000/api/v2/pages/
, but instead I get an empty result:
{
"meta": {
"total_count": 0
},
"items": []
}
I’m not sure what my mistake is. Could it possibly be an error in my model definitions, API settings, or another issue? Has anyone faced a similar problem and figured out a solution?
Here’s a brief look at my ProdPagesAPIViewSet for reference:
class ProdPagesAPIViewSet(PagesAPIViewSet):
renderer_classes = [JSONRenderer]
model = FlexPage
api_router.register_endpoint("pages", ProdPagesAPIViewSet)
Any insights would be greatly appreciated.
hey, i had this problem too. check if ur pages are actually published. sometimes they stay in draft mode n don’t show up in the API. also, make sure ur FlexPage is a subclass of Page. if that doesn’t work, try clearing ur cache or restarting the server. good luck!
Have you verified that your API endpoint is correctly configured in your project’s URLs? Sometimes the issue lies in the routing. Make sure you’ve included the API URLs in your project’s main urls.py file:
from wagtail.api.v2.router import WagtailAPIRouter
from .api import api_router
urlpatterns = [
…
path(‘api/v2/’, api_router.urls),
…
]
Also, double-check that your FlexPage model is properly registered with Wagtail’s page types. In your models.py, ensure you have:
from wagtail.core.models import Page
class FlexPage(Page):
…
If these aren’t the issue, consider enabling debug logging for the API to get more detailed information about what’s happening behind the scenes. You can do this by adding the following to your settings.py:
LOGGING = {
‘version’: 1,
‘disable_existing_loggers’: False,
‘handlers’: {
‘console’: {
‘class’: ‘logging.StreamHandler’,
},
},
‘loggers’: {
‘wagtail.api’: {
‘handlers’: [‘console’],
‘level’: ‘DEBUG’,
},
},
}
This might provide more insight into why your pages aren’t appearing in the API response.
I ran into a similar issue when I first started working with Wagtail’s API. After some troubleshooting, I discovered that the problem was related to page permissions. By default, Wagtail’s API only returns pages that are accessible to the current user.
To fix this, I had to modify my API configuration to explicitly allow public access to the pages. In your api.py file, try adding the following to your ProdPagesAPIViewSet:
class ProdPagesAPIViewSet(PagesAPIViewSet):
renderer_classes = [JSONRenderer]
model = FlexPage
authentication_classes = []
permission_classes = []
This removes authentication and permission checks, allowing all pages to be visible through the API. However, be cautious with this approach if you have sensitive content.
Another thing to check is whether your FlexPage model is a subclass of Page. If it’s not, the API might not recognize it as a valid page type.
Lastly, ensure that you have at least one published FlexPage. Draft pages won’t show up in the API by default. If you’re still having issues after trying these solutions, you might want to check your Wagtail version and make sure it’s up to date.