在另一个Wagtail pag中嵌入表单生成器页

2024-04-28 08:30:28 发布

您现在位置:Python中文网/ 问答频道 /正文

Wagtail Form Builder文档说明:

form_page.html differs from a standard Wagtail template in that it is passed a variable form, containing a Django Form object, in addition to the usual page variable.

但是在我当前的项目中,我需要在另一个页面中嵌入一个联系人表单(实现为表单生成器页面)。在该目标页面的模型中,我使用PageChooserPanel让编辑器选择要嵌入的表单页面。换句话说:

class TargetPage(Page):
    [...snip...]  
    contact_form = models.ForeignKey(
        'wagtailcore.Page',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )

    content_panels = Page.content_panels + [
        [...snip...]
        PageChooserPanel('contact_form', 'FormPage'),
    ]

我的问题是目标页面的模板。我可以通过page.contact_form访问表单页的属性,但由于此页没有传入form对象,因此我无法确定如何呈现字段。你知道吗

我猜我需要重写目标页的get\u context(),以便它包含我需要的form对象。但我不知道怎么弄到那个东西。善良的灵魂能让我走上正轨吗?你知道吗


Tags: informtrue表单目标modelspagecontact
1条回答
网友
1楼 · 发布于 2024-04-28 08:30:28

经过一夜的睡眠,答案变得相对明显。缺少的链接是Wagtail FormPage的get\u form()方法。我现在在我的TargetPage模型中使用这个:

def attached_form(self):
    return self.contact_form.specific.get_form()

因此,在我的模板中,我可以参考所附的表格来获取我的字段。你知道吗

相关问题 更多 >