0

I am using a page model 'EventPage', which shall have a slug based on the id of the page. To achieve this, I have modified the full_clean method like this (similar to this question):

class EventPage(Page):
    ...
    
    def full_clean(self, *args, **kwargs):
        super().full_clean(*args, **kwargs)

        # set slug to id, if already existing
        if self.id is not None:
            self.slug = str(self.id)

This seems to work fine in principle. However, after the page is published,the Wagtail admin/pages view shows a message box at the top ('Page ... created and published') with a View live button that links to the wrong url (i.e. using the default slug created from the page title).

In the list of pages below that, the just created page's own View live and Add child page links show the correct page url using the page id. It is just the message box's View live url at the top that needs to be corrected. Here's a screenshot:

enter image description here

How can I get the correct link in the message box at the top as well?

In case it matters, I am currently using Wagtail 2.9, Django 2.2.9 and Python 3.6. I guess my problem has something to do with the fact that the page id is not known until the page is saved for the first time, and the View live link in the message box somehow uses an initial page.url which is overridden later on. Any ideas how to solve this?

Kristin
  • 155
  • 4

2 Answers2

1

You could probably achieve this by using Hooks in Wagtail. Hooks are typically used to customize the view-level behaviour of the Wagtail admin and front end. I would suggest overwriting the button functionality this way, or possibly removing the existing button and adding your own new one in. More info in the docs: https://docs.wagtail.io/en/latest/reference/hooks.html

kbdev
  • 1,225
  • 1
  • 13
  • 33
  • Thanks, good idea! I had hoped to fix it without hooks, but I'll give it a try. – Kristin Oct 28 '20 at 22:03
  • I had tried it with the Wagtail hooks, but couldn't get it to work with e.g. 'after_publish_page', because I couldn't get the correct live url. It seems one needs to use Django signals instead, see https://stackoverflow.com/questions/70515946/ and the accepted answer, which also solved the issue for me. – Kristin Sep 15 '22 at 17:54
0

For anyone ending up here, the solution was given in the accepted answer to a similar question here: Wagtail 'View live' button provides wrong url after page creation while using id as slug.

In short: use Django signals instead of hooks, i.e. here a the post_save signal for EventPage. For documentation purposes, I'm repeating here the solution from the linked question, slightly adjusted. All credits and thanks go to gasman.

Create following signal in the calendar app, calendar/signals.py:

from django.db.models.signals import post_save
from django.dispatch import receiver

from .models import EventPage

@receiver(post_save)
def set_number_and_slug_after_event_page_created(sender, instance, created, **kwargs):
    if issubclass(sender, EventPage) and created:
        page = instance
        page.number = page.slug = str(page.id)
        page.save()
        new_revision = page.save_revision()
        if page.live:
            new_revision.publish()

And, if not yet done, register signals in the corresponding calendar/apps.py config:

from django.apps import AppConfig

class CalendarConfig(AppConfig):
    name = 'calendar'

    def ready(self):
        from . import signals

This worked like a charm for me.

Kristin
  • 155
  • 4