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)?
Get page URL for scheduled publishing
136 Views Asked by Vlax At
2
There are 2 best solutions below
0
On
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)
slug_urlshould give you what you need: https://docs.wagtail.org/en/latest/topics/writing_templates.html?highlight=slugurl#slugurl-tagNote that
slugurlmight 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 thetest_slugurl_tag()routine in the tests for an example of how to useslugurlin Python code.Note also that if the location of the page in the page tree is changed or the slug (as defined in the
Promotetab) is changed, then the url that you retrieved withslugurlprior 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 thewagtail.contrib.redirectsapp.