'Wagtail 'View live' button provides wrong url after page creation while using id as slug

I have a case that uses Page ID as a slug, after creating a new Page, Wagtail provides us a "View live" button but when we click on that button, it provides a wrong URL

enter image description here

enter image description here

The right URL should be ".../property-list/<property-id>"

I have searched on stack overflow, found this thread but the answer is still a mystery: Wrong 'View live' url in Wagtail admin message after page creation when using id as slug

I have followed the Wagtail official document, using Wagtail Hooks to manipulate data. However, no success yet. This is my code:

@hooks.register('after_create_page')
def set_number_and_slug_after_property_page_created(request, page):
    page.number = page.slug = str(page.id)
    page.save()
    new_revision = page.save_revision()
    if page.live:
        new_revision.publish()

Please help me, thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source