matousc
matousc

Reputation: 3977

Wagtail url prefix for a custom Page model

This question is probably trivial, but I am unable to see a simple solution.

I have custom page model representing Post:

class PostPage(Page):

I would like to make all instances of this model (all Posts) accessible only with url prefix

/posts/

Example:

User creates new Post, the assigned slug will be

awesome-first-post

What should happen is, that

/awesome-first-post/

will result in 404, while

/posts/awesome-first-post/

will display the post.

Note: I want this prefix only for the specific model Postpage. Other pages should be served directly from their slug.

Upvotes: 3

Views: 425

Answers (1)

gasman
gasman

Reputation: 25227

In Wagtail, page URLs are formed from the list of slugs of the page's parent and ancestor pages, based on the page's position in the tree - the developer doesn't specify them directly. So, to get the URL /posts/awesome-first-post/, create a page with the slug posts (usually you'd create a dedicated PostIndexPage page model to serve as a listing page), and create the page awesome-first-post as a child of that one (by clicking the '+' icon next to the Posts page in the explorer listing view).

If you want to make sure that users only ever create PostPages as children of the PostIndexPage, use a subpage_types / parent_page_types setting, for example:

class PostPage(Page):
    # ...
    parent_page_types = ['PostIndexPage']

Upvotes: 5

Related Questions