user8703143
user8703143

Reputation: 173

Wagtail - made custom slug but page not serving from it

I made a custom slug because the default slug does not meet my needs and already set the url_path with the new slug. But the page is still serving with the default slug.

I've already set the url_path with the following code:

def set_url_path(self, parent):
    super().set_url_path(self)
    if parent:
        self.url_path = parent.url_path + self.custom_slug + '/'
    return self.url_path

But when I publish the page, it still returns the page with the default slug value. I thought Wagtail serves the page on url_path. What other methods or variables do I have to override for the page to be served on my custom slug?

Upvotes: 1

Views: 1296

Answers (1)

allcaps
allcaps

Reputation: 11248

You can not switch to some other field then slug. The slug field is used all over Wagtail to lookup url's, queries and serve the correct page.

You can programatically set the slug value to something else. The Page.slug definition is:

slug = models.SlugField(
    verbose_name=_('slug'),
    allow_unicode=True,
    max_length=255,
    help_text=_("The name of the page as it will appear in URLs e.g 
                 http://example.com/blog/[my-slug]/")
)

The field accepts unicode chars.

You should override Page._get_autogenerated_slug, Page.full_clean and Page.clean. Change how slug is handled and adopt to your wishes.

Note you can use some other field eg my_custom_slug as an input and use this value in your custom methods. Eg: self.my_custom_slug. However, the final result has to be stored in the slug field.

You can remove the slug field from the content panels to remove it from the admin user interface.

Finally, the slug has to comply with https://www.rfc-editor.org/rfc/rfc1738

Upvotes: 1

Related Questions