有没有办法在Wagtail中继承抽象父模型class的模板?

Is there a way to inherit the template of an abstract parent model class in Wagtail?

我有一个抽象的 Page 模型,它定义了一种页面类型的公共字段,然后我将其子类化以限制允许的子页面类型。我希望这个抽象模型的所有子类都默认使用抽象模型中定义的 template,但它们似乎没有。

class BaseListing(Page):
    empty_message = RichTextField()
    intro = RichTextField()

    template = 'listing.html'

    class Meta:
        abstract = True


class BlogListing(BaseListing):
    subpage_types = ['BlogPost']

我希望 wagtail 默认使用 BaseListing 模型中定义的模板,但它会寻找 blog_listing.html 模板,除非我在 BlogListing 像这样的模型:

class BlogListing(BaseListing):
    subpage_types = ['BlogPost']
    template = 'listing.html'

在您的抽象页面模型上定义 a get_template method。通常,get_template 的默认实现将简单地 return self.template (这又默认为从 class 名称派生的文件名,给出您当前看到的行为) .

get_template 最常见的用途是根据每个请求改变模板(例如,为经过身份验证的用户提供不同的模板);但是,如果您将其定义为 return 一个固定的模板名称,这将覆盖每个子 class 获取其自己的模板的标准行为。