为w的模型的每一页修改段塞 [英] Modify slug for every page of model on wagtail
问题描述
我试图为Wagtail上的特定Page模型的每个页面创建一个自定义标签.我一直在调查并尝试 RoutablePageMixin 示例,但这似乎修改了url结构,而不是段本身.
I am trying to create a custom slug for every page of a specific Page model on Wagtail. I have been looking into and tried the RoutablePageMixin examples, but that seems to modify the url structure, not the slug itself.
这就是我想要做的:
当前页面标题:关于Django
Current page title: About Django
当前页面提示:about-django
current page slug: about-django
我想要的页面标题:关于Django
page title I want: About Django
我想要的页面:awesome-things-about-django
page slug I want: awesome-things-about-django
我看过创建路由方法,但这似乎并没有修改.有什么办法可以将一些东西放在您的鼻涕虫上吗?我知道我可以手动执行此操作,但希望它可以自动执行.
I looked at creating route method, but that didn't seem to modify the slug. Is there any way to prepend something onto your slug? I understand I can do this manually, but would prefer for it to happen automatically.
推荐答案
保存时自动更新页面的一种可能性是覆盖 full_clean
方法:
One possibility for making automatic updates to pages on save is to override the full_clean
method:
class MyPage(Page):
# ...
def full_clean(self, *args, **kwargs):
# first call the built-in cleanups (including default slug generation)
super(MyPage, self).full_clean(*args, **kwargs)
# now make your additional modifications
if not self.slug.startswith('awesome'):
self.slug = "awesome-%s" % self.slug
这篇关于为w的模型的每一页修改段塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!