How to correct link to "Live" from admin

72 views
Skip to first unread message

Isaac Jordan

unread,
Jan 27, 2016, 4:21:33 PM1/27/16
to Wagtail support
I have overridden the following functions on my BlogPost model to give my posts a /blog/year/month/slug link style:

def route(self, request, path_components):
        if path_components:
            reqYear = path_components[0]
            reqMonth = path_components[1]

            if int(reqYear) == self.date.year and int(reqMonth) == self.date.month:
                if self.live:
                    return RouteResult(self)
        raise Http404

def url(self):
    return self.date.strftime("%Y") + "/" + self.date.strftime("%m") + "/" + self.slug

However, when I click the "Live" link in Wagtail CMS it directs me to /blog/slug/.

Is there a method I need to override to change this link?

Isaac Jordan

unread,
Feb 8, 2016, 11:08:25 AM2/8/16
to Wagtail support
Anyone have an idea with this? I haven't managed to solve it yet.

Matthew Westcott

unread,
Feb 8, 2016, 12:02:14 PM2/8/16
to wag...@googlegroups.com
Hi Isaac,
Unfortunately this is a limitation of the admin interface, which exists for performance reasons: to retrieve the page listing in a single database query, it fetches them as basic 'Page' instances, which means that it won't see any custom URL logic that you've implemented on a specific page class.

As it happens I've got a task lined up this week about making this listing more customisable <https://github.com/torchbox/wagtail/issues/2196>, and this might be something I can fix along the way - will see what I can do.

Cheers,
- Matt

Isaac Jordan

unread,
Feb 8, 2016, 12:05:28 PM2/8/16
to Wagtail support
Thanks Matt.

The expected behaviour from my point of view would be that it calls get_absolute_url() on each page (or at least url()) for each page in the listing. I'll follow that ticket on GitHub to keep up to date.

Matthew Westcott

unread,
Feb 8, 2016, 12:06:05 PM2/8/16
to wag...@googlegroups.com
One (slightly hacky) way of doing what you want might be to to implement a custom serve() method that redirects to the expected URL if you visit it via another URL:

def serve(self, request):
if request.path != self.url:
return HttpResponseRedirect(self.url)

return super(BlogPost, self).serve(request)


Cheers,
- Matt


On 8 Feb 2016, at 16:08, Isaac Jordan <shee...@gmail.com> wrote:

Isaac Jordan

unread,
Feb 8, 2016, 3:43:23 PM2/8/16
to Wagtail support
Awesome, thanks. I managed to get it working (but I had to use self.get_absolute_url() instead of self.url(), since request.path seems to be the full absolute url).
Reply all
Reply to author
Forward
0 new messages