将 Wagtail 表单字段查询到主页

Query Wagtail Form Fields into Homepage

我想知道是否可以将抽象 wagtail 表单中的表单字段查询到页面模型中。我想在我的主页(页面)模型上放置一个 wagtail 表单,但我真的不明白你将如何实现这一点,如果可能的话。

欢迎任何反馈

home_page.html

  ...
  <form action="{% pageurl page %}" method="POST">
          {% csrf_token %}
      <div class="row">
        <div class="col-md-12 col-sm-12">

        {% if form.non_field_errors %}
            <div class="alert alert-danger" role="alert">
              {% for error in form.non_field_errors %}
                {{ error }}
              {% endfor %}
            </div>
              {% endif %}

        <h3>Contact Form</h3><br>
        </div>

       {% for item in page.formPage.all %}
        {% for field in form.visible_fields %}
        <div class="col-md-6 col-sm-12">
          <div class="form-group">
            {% render_field field class+="form-control" %}
          </div>
        </div>
        {% endfor %}
      {% endfor %}
        </div>
        <div class="pull-center">
          <button type="submit" class="btn btn-contact mt-3">Submit</button>
        </div>
    </form>

models.py

imports removed

class HomePage(Page):
    firstheader = RichTextField(blank=True)
    firstbody = RichTextField(blank=True)
    registerbuttonurl = models.CharField(blank=True, max_length=250)
    secondheader = RichTextField(blank=True)
    secondbody = RichTextField(blank=True)
    registerlink = RichTextField(blank=True)

    def Form(self):
        return FormPage.objects.all()

    content_panels = Page.content_panels + [

        FieldPanel('firstheader', classname="full"),
        FieldPanel('firstbody', classname="full"),
        FieldPanel('registerbuttonurl'),
        FieldPanel('secondheader', classname="full"),
        FieldPanel('secondbody', classname="full"),
        FieldPanel('registerlink', classname="full"),
        InlinePanel('gallery_images', label="Certified Distributor Logos"),
    ]



class FormField(AbstractFormField):
    page = ParentalKey('FormPage', on_delete=models.CASCADE, related_name='form_fields')


class FormPage(AbstractEmailForm):
    intro = RichTextField(blank=True)
    thank_you_text = RichTextField(blank=True)

    content_panels = AbstractEmailForm.content_panels + [
        FieldPanel('intro', classname="full"),
        InlinePanel('form_fields', label="Form fields"),
        FieldPanel('thank_you_text', classname="full"),
        MultiFieldPanel([
            FieldRowPanel([
                FieldPanel('from_address', classname="col6"),
                FieldPanel('to_address', classname="col6"),
            ]),
            FieldPanel('subject'),
        ], "Email"),
    ]


    thank_you_page = models.ForeignKey(
        'wagtailcore.Page',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )

    def render_landing_page(self, request, form_submission=None, *args, **kwargs):
        ...removed

return FormPage.objects.all() 将不起作用,因为表单字段是在您在管理中创建的一个特定 FormPage 实例上定义的,而不是在 FormPage class 本身上定义的.您的代码中需要一些东西来确定您指的是哪个 FormPage - 我建议给它一个独特的 slug(本例中的 contact-us)并根据它检索它。

class HomePage(Page):
    # ...
    def get_contact_form_page(self):
        return FormPage.objects.get(slug='contact-us')

    def get_contact_form(self):
        return self.get_contact_form_page().get_form()

然后,在您的主页模板上,您可以访问 page.get_contact_form 并以与在 FormPage 自己的模板上呈现它相同的方式呈现生成的表单:

{% with page.get_contact_form as form %}
    <form action="{% pageurl page.get_contact_form_page %}" method="POST">
        {% csrf_token %}
        {% for field in form.visible_fields %}
            ...
        {% endfor %}
    </form>
{% endwith %}

注意表单动作设置为post到表单页面;把{% pageurl page %}放在这里会导致它post回到首页,首页不知道如何处理表单提交。同样,用于显示验证错误消息的代码可以从主页模板中删除 - 如果用户提交无效表单,它们将被放到联系我们页面而不是主页上。