Specifying URLs for select Page types

168 views
Skip to first unread message

Ian Jeffries

unread,
Jul 10, 2014, 2:23:47 PM7/10/14
to wag...@googlegroups.com
Say I was making a blog with Wagtail, and I wanted each blog post to be located at `<date>/<urlized-title>`, e.g. `2014/06/10/wagtail-is-nice`. Is there a way to do this where the blog post writer doesn't have to create a `2014` Page, then an `06` Page, etc? If so could you point me to the relevant docs or give a quick explanation?

Matthew Westcott

unread,
Jul 14, 2014, 9:36:35 AM7/14/14
to wag...@googlegroups.com
Hi Ian,
That sounds like a job for a custom 'route' method:
http://wagtail.readthedocs.org/en/latest/model_recipes.html#adding-endpoints-with-custom-route-methods

You would have a BlogIndex page that lives at the top level, and BlogPost pages directly underneath it (or somewhere entirely different - it doesn't matter in this case). Then, BlogIndex would have a custom 'route' method that looks at the path_components parameter (which would be something like ['2014', '06', '10', 'wagtail-is-nice']) and uses those in a query to fetch the appropriate page. It would then return a pointer to that page, as RouteResult(some_page).

You'd probably also want to override BlogPost's 'url' method, to return URLs in this format rather than the default one based on its location in the tree.

(Another thing to keep an eye on is this pull request <https://github.com/torchbox/wagtail/pull/254>, which should hopefully be arriving in Wagtail in the near future - it will allow you to do these kinds of routing arrangements using Django's own urlconf mechanism.)

- Matt

Ian Jeffries

unread,
Jul 21, 2014, 12:49:51 PM7/21/14
to wag...@googlegroups.com
This worked great, thanks so much!
Reply all
Reply to author
Forward
0 new messages