Wagtail: Force slug in models.py

Created on 11 Jul 2016  路  3Comments  路  Source: wagtail/wagtail

Hello everyone,

is it possible to force a slug in the Page model?
The documentation says there is a slug variable in the class ( http://docs.wagtail.io/en/v1.5.2/reference/pages/model_reference.html#wagtail.wagtailcore.models.Page#slug ) but setting it doesn't do anything or trigger a migration demand.

My use case is having a {% slugurl 'homepage' %} for the HomePage(Page) model in the base.html without worrying that someone could change the slug to something else later on. Also I want to have some more sites that will absolutely have a specific, unchangeable slug that I want to set in the models.py.

Is this possible somehow?

Most helpful comment

You can setup required slugs using migrations. Here is an example how to create a home page using migration.

If you want to disable ability to edit the slug, you can hide slug field from the page edit view. It can be done somehow like this:

HomePage.promote_panels = [
    MultiFieldPanel([
        FieldPanel('seo_title'),
        FieldPanel('show_in_menus'),
        FieldPanel('search_description'),
    ], 'Common page configuration'),
]

See editor panels documentation for more details.

All 3 comments

You can setup required slugs using migrations. Here is an example how to create a home page using migration.

If you want to disable ability to edit the slug, you can hide slug field from the page edit view. It can be done somehow like this:

HomePage.promote_panels = [
    MultiFieldPanel([
        FieldPanel('seo_title'),
        FieldPanel('show_in_menus'),
        FieldPanel('search_description'),
    ], 'Common page configuration'),
]

See editor panels documentation for more details.

I happened across this issue because I'm attempting to modify the slug field within the promote panel so that it isn't editable in certain situations.

@m1kola The link to the editor panels documentation is broken in your post. Was your link pointing to what is mentioned here: https://docs.wagtail.io/en/v2.5.1/topics/pages.html?highlight=editor%20panels#editor-panels

And if it was, do you have any documentation links that describe what we have available to us to edit within the content_panels, promoe_panels, and settings_panels objects? For example, in your post you state the names of fields that appear to be those that are already provided to us by Wagtail, but I don't know how you learned of those names to begin with.

Thanks, @anilnatha I updated my post with a link to a new version of docs.

And if it was, do you have any documentation links that describe what we have available to us to edit within the content_panels, promoe_panels, and settings_panels objects?

You can specify any editable fields available in your model including these defined in Wagtail's the base Page model. You can find fields reference doc for the Page model here or in the source code:

https://github.com/wagtail/wagtail/blob/a764bac61a645386368e36d15dfbcc555d92c5f5/wagtail/core/models.py#L228-L321

Was this page helpful?
0 / 5 - 0 ratings