Wagtail empty results in pages api
I am getting empty results in wagtail api pages list.
my models.py
from django.db import models
from wagtail.models import Page
from wagtail.admin.panels import FieldPanel
from wagtail.api import APIField
from wagtail.fields import StreamField
from wagtail.admin.panels import FieldPanel
from streams import blocks
class FlexPage(Page):
banner_title = models.CharField(blank=True, max_length=200)
banner_text = models.CharField(blank=True, max_length=2000)
banner_cta_text = models.CharField(blank=True, max_length=100)
banner_cta_url = models.URLField(blank=True, default="")
banner_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
content = StreamField(
[
("youtube_block", blocks.YouTubeBlock()),
("image_block", blocks.ImgBlock()),
("section_heading", blocks.SectionHeading()),
("title_text_two_columns", blocks.TitleTextTwoColumns()),
],
null=True,
blank=True,
use_json_field=True,
)
content_panels = Page.content_panels + [
FieldPanel("banner_title", classname="full"),
FieldPanel("banner_text"),
FieldPanel("banner_cta_text"),
FieldPanel("banner_cta_url"),
FieldPanel("banner_image"),
FieldPanel("content"),
]
api_fields = [
APIField("banner_title"),
APIField("banner_text"),
APIField("banner_cta_text"),
APIField("banner_cta_url"),
APIField("banner_image"),
APIField("content"),
]
blocks.py
from wagtail import blocks
from wagtail.blocks.field_block import (BooleanBlock, CharBlock,
PageChooserBlock, TextBlock, URLBlock)
from wagtail.images.blocks import ImageChooserBlock as DefaultImageChooserBlock
class ImageChooserBlock(DefaultImageChooserBlock):
def get_api_representation(self, value, context=None):
if value:
return {
"id": value.id,
"title": value.title,
"original": value.get_rendition("original").attrs_dict,
"thumbnail": value.get_rendition("fill-120x120").attrs_dict,
}
class YouTubeBlock(blocks.StructBlock):
url = URLBlock(required=True)
class Meta:
template = "streams/youtube.html"
label = "Youtube"
class ImgBlock(blocks.StructBlock):
image = ImageChooserBlock()
cta = URLBlock(required=False)
class Meta:
template = "streams/image_block.html"
label = "Image"
class SectionHeading(blocks.StructBlock):
title = blocks.CharBlock(required=True, help_text="add your title")
class Meta:
template = "streams/section_heading.html"
label = "section heading"
class TitleTextTwoColumns(blocks.StructBlock):
title = blocks.CharBlock(required=False)
content = blocks.CharBlock(required=False)
left_title = blocks.TextBlock(required=False, max_length=500)
left_text = blocks.TextBlock(required=False, max_length=1500)
left_image = ImageChooserBlock(required=False)
left_image_cta = blocks.URLBlock(required=False)
right_title = blocks.TextBlock(required=False, max_length=500)
right_text = blocks.TextBlock(required=False, max_length=1500)
right_image = ImageChooserBlock(required=False)
right_image_cta = blocks.URLBlock(required=False)
class Meta:
template = "streams/title_text_two_columns.html"
label = "title Text Two Columns"
api.py
# api.py
from wagtail.api.v2.views import PagesAPIViewSet
from common.models import FlexPage # Adjust the import path as necessary
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.images.api.v2.views import ImagesAPIViewSet
from wagtail.documents.api.v2.views import DocumentsAPIViewSet
# Create the router. "wagtailapi" is the URL namespace
api_router = WagtailAPIRouter('wagtailapi')
from rest_framework.renderers import JSONRenderer
...
class ProdPagesAPIViewSet(PagesAPIViewSet):
renderer_classes = [JSONRenderer]
model=FlexPage
# name = "pages"
api_router.register_endpoint("pages", ProdPagesAPIViewSet)
# Add the three endpoints using the "register_endpoint" method.
# The first parameter is the name of the endpoint (such as pages, images). This
# is used in the URL of the endpoint
# The second parameter is the endpoint class that handles the requests
# api_router.register_endpoint('pages', PagesAPIViewSet)
api_router.register_endpoint('images', ImagesAPIViewSet)
api_router.register_endpoint('documents', DocumentsAPIViewSet)
now i created a page and published it. i was expecting it in http://localhost:8000/api/v2/pages/
but i got
{
"meta": {
"total_count": 0
},
"items": []
}
where did i go wrong?