Danny
Danny

Reputation: 510

How to Django reverse to pages created in Wagtail?

I have a Wagtail model for an object.

    class ObjectPage(Page):
        # fields, not really important

I want to make the object editable by front-end users, so I have also created a generic update view to accomplish this. My question is how I can use Django's reverse() function to point to the edited object in my get_success_url() method:

    class EditObjectPage(LoginRequiredMixin, UpdateView):
        # model, template_name, and fields defined; not really important

        def get_success_url(self):
            return("ObjectPage", kwargs={"slug" : self.object.slug})   # doesn't work
            return("object_page_slug", kwargs={"slug" : self.object.slug})   # also doesn't work

I know how to do this when I'm explicitly defining the URL name in my urls.py file, but in this case, the URL is created as a Wagtail page and is handled by this line of my urls.py:

url(r"", include(wagtail_urls)),

I've been searching the documentation for whether Wagtail pages are assigned names that can be reversed to, but I'm finding nothing in the official documentation or here on StackOverflow.

Upvotes: 0

Views: 778

Answers (1)

gasman
gasman

Reputation: 25292

The page object provides a get_url method - get_success_url should be able to accept this directly, so there's no need to use reverse.

def get_success_url(self):
    return self.object.get_url()

Internally the URL route used by get_url is wagtail_serve, so if you absolutely need to go through reverse, you could replicate the logic there.

Upvotes: 3

Related Questions