1
class ProductIndexPage(RoutablePageMixin, Page):
    subpage_types = ['ProductPage']
    parent_page_types = ['home.HomePage']

    def live_categories(self):
        all_products_live_id = ProductPage.objects.live().values_list('categories_id', flat=True)
        list_live_id_uniqe = list(set(all_products_live_id))
        live_cat = ProductCategory.objects.filter(id__in=list_live_id_uniqe).order_by('-id')
        return live_cat

    @path('')
    @path('all-categories/')
    def all_category_page(self, request):
        productpages = self.get_children().live().order_by('-first_published_at')

        return self.render(request, context_overrides={
            'title': self.title,
            'productpages': productpages,
            'live_categories': self.live_categories,
        })

    @path('<str:cat_name>/', name='cat_url')
    def current_category_page(self, request, cat_name=None):
        productpages = ProductPage.objects.live().filter(categories__slug__iexact=cat_name).order_by \
            ('-first_published_at')
        current_cat = self.live_categories().get(slug=cat_name).name
        return self.render(request, context_overrides={
            'title': "%s" % current_cat,
            'productpages': productpages,
            'live_categories': self.live_categories,
        })

    @path('<str:cat_name>/<str:prod_name>/')
    def product_page(self, request, cat_name=None, prod_name=None):
        product = ProductPage.objects.get(categories__slug__iexact=cat_name, slug=prod_name)
        return self.render(request, context_overrides={
            'product': product},
            template="products/product_page.html",)

I can't edit page in wagtail admin menu from path:

@path('<str:cat_name>/<str:prod_name>/')

My page tree in admin: root/products/<prod_name> How to change route in Admin for editing pages from wagtal button or for preview in Admin? I am newbie, plz show me example. sorry for English)

1 Answers1

0

With RoutablePageMixin there is only one page - and it displays differently depending on the parameter sent on the url. Unfortunately, this means you can't preview the category version of your page, current_category_page.

But it looks like the product page you are routing too as @path('str:cat_name/str:prod_name/') should be displaying the same page as you serve from the ProductPage's own url. So the ProductPage you see in the preview ProductIndexPage + 'str:cat_name/str:prod_name/'

cnk
  • 981
  • 1
  • 5
  • 9
  • When i'm on page (ProductIndexPage + 'str:cat_name/str:prod_name/') and click on wagtal admin button/edit this page/ i iget editing ProductIndexPage. How rewrite this route in function. And in loginning Admin i can't open this page - i ge ProductIndexPage/prod_name (child page) – Jordano Bruno Nov 21 '22 at 18:29
  • I think you are referring to links in the User Bar. One can make changes to those using the [construct_wagtail_userbar hook](https://docs.wagtail.org/en/latest/reference/hooks.html#construct-wagtail-userbar). – cnk Nov 22 '22 at 22:41