有没有办法覆盖 FieldPanel 上的标签和帮助文本?

Is there a way to override the label and help text on a FieldPanel?

我想更改管理界面中某些 Page 字段的帮助文本和标签。 FieldPanel 似乎通常从模型字段中获取标签和帮助文本,但由于我想更改 Page 模型(titlesearch_description,特别是),我无法在字段本身

上设置 verbose_namehelp_text

我尝试将 headinghelp_text 关键字参数传递给 FieldPanel,但我仍然在管理界面中看到默认标签和帮助文本。

class MyPage(Page):
    content_panels = [
        FieldPanel('title', heading='Name', classname='full'),
        FieldPanel('search_description', heading='Description',
                   classname='full',
                   help_text='Description used in indices and search results')
    ]

我不知道有什么 简单 方法可以将帮助文本直接添加到单个字段中,但是您可以将这些字段包装在 MultiFieldPanel 中,然后将标题:

class MyPage(Page):
    content_panels = [
        MultiFieldPanel([
            FieldPanel('title', heading='Name', classname='full'),
            FieldPanel('search_description', heading='Description',
                       classname='full',
                       help_text='Description used in indices and search results')
        ], heading="your help text")
    ]

wagtail.core.models.py你会看到:

from django.utils.translation import ugettext_lazy as _

class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
    title = models.CharField(
        verbose_name=_('title'),
        max_length=255,
        help_text=_("The page title as you'd like it to be seen by the public")
    )

由于 Page 不是摘要 class,您 can't override its fields in your own Page-based classes even if you try to redefine title. Also, notice the import of ugettext_lazy as _ and then the _('title') in the verbose_name declaration. This answer 解释说此代码正在获取 titleverbose_name 的翻译版本.

来自 @KalobTaulien 在 Wagtail Slack 中的建议似乎可以解决问题:

class MyPage(Page):
    pass

MyPage._meta.get_field('title').verbose_name = 'name'
MyPage._meta.get_field('search_description').help_text = 'Description used in indices and search results'

有一种方法可以在每个字段的基础上覆盖默认的 help_text 和标签(称为 verbose_name)。

MyPage._meta.get_field("title").help_text = "Help me Obi-Wan, you're my only help_text"
MyPage._meta.get_field("title").verbose_name = "Jedi Labelling"

有这个方法。然后还有 MultiFieldPanel 的 heading,Dan Swain 在他的回答中很好地介绍了这一点。

如果所有这些都不太奏效,那么 Wagtail HelpPanel 总是有用的。 http://docs.wagtail.io/en/v2.6.1/reference/pages/panels.html#helppanel

的文档中有更多相关信息

如果您喜欢从视频中学习,我还围绕这个主题创建了 a YouTube video

希望这对您有所帮助!