Let's say I've created a blog for my website. The tree structure setup in the Wagtail admin looks like this:

Homepage > Blog Index > Blog Post

Is it possible to keep the Blog Index page in the admin page tree but remove it from the URL so that my URL's look like this:

Homepage > Blog Post

I am assigning a custom group to the Blog Index page which allows them to edit only Blog Posts that they have created, which is why Blog Index needs to stay in the tree on the admin side.

I've done a little work with the routablepagemixin but not to eliminate anything from the URL, only add to it.

1 Answers

3
Kalob Taulien On Best Solutions

I'm not entirely certain if a RoutablePageMixin is the right way to go about this, but that's how I've solved this before.

Here's an example of how you could do it with a RoutablePageMixin and a route (Note: I chopped this together pretty quickly and didn't test it, you might need to do some adjusting)

from django.http import HttpResponseRedirect

from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.core.models import Page

from blog.models import BlogPage


class HomePage(RoutablePageMixin, Page):
    """A home page class."""

    # HomePage Fields here...

    # This route will collect the blog slug
    # We'll look for the live BlogPost page.
    @route(r"^(?P<blog_slug>[-\w]*)/$", name="blog_post")
    def blog_post(self, request, blog_slug, *args, **kwargs):
        try:
            # Get the blog page
            blog_page = BlogPage.objects.live().get(slug=blog_slug)
        except BlogPage.DoesNotExist:
            # 404 or post is not live yet
            return HttpResponseRedirect("/")
        except Exception:
            # Handle your other exceptions here; here's a simple redirect back to home
            return HttpResponseRedirect("/")

        # Additional logic if you need to perform something before serving the blog post

        # Let the blog post page handle the serve
        return blog_page.specific.serve(request, *args, **kwargs)

One other thing to note: you'll want to change the sitemap url on your original blog post pages so they don't show up as /blog/blog-slug/ inside of sitemap.xml.