My editors want to know the end page url for publishing in social media. Is there any way to get the page URL as when it will be published before actually publishing it (scheduled publishing)?
2 Answers
slug_url
should give you what you need:
https://docs.wagtail.org/en/latest/topics/writing_templates.html?highlight=slugurl#slugurl-tag
Note that slugurl
might provide a relative link, so if it does, then you would have to prepend the domain name to the relative url in order to create the final url for the user. See the test_slugurl_tag()
routine in the tests for an example of how to use slugurl
in Python code.
Note also that if the location of the page in the page tree is changed or the slug (as defined in the Promote
tab) is changed, then the url that you retrieved with slugurl
prior to any such change will no longer be valid. However, if you are using Wagtail 2.16+, then this problem can be handled automatically if you use the wagtail.contrib.redirects
app.

- 2,910
- 1
- 16
- 36
-
Thanks for the answer, it's actually a bit more complicated I'll post a complete solution soon – Vlax Feb 21 '22 at 21:13
Here a code snippet that takes in account when the slug changes or the page is moved in the tree (you will need to subclass the Page model somehow and add a new field published_url
):
def save_revision(self, user=None, submitted_for_moderation=False, approved_go_live_at=None, changed=True,
log_action=False, previous_revision=None, clean=True):
old_record = Page.objects.get(id=self.id)
if old_record.slug != self.slug:
kwargs = {'update_fields': ['slug']}
Page.save(self, kwargs)
self.published_url = self.get_full_url()
return Page.save_revision(self, user, submitted_for_moderation, approved_go_live_at, changed,
log_action, previous_revision, clean)

- 1,447
- 1
- 18
- 24