【发布时间】:2018-09-28 23:47:26
【问题描述】:
我想知道是否可以从抽象的 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
【问题讨论】:
-
“将表单字段查询到页面模型中”是什么意思?您的意思是要根据表单字段的内容创建一个新页面吗?是否要对之前的表单提交进行查询并在页面上显示输出?或者您只是想在页面上显示表单 - 在这种情况下,您能告诉我们您现有代码中目前哪些地方不起作用吗?
-
我在models.py中使用了“def Form(self): return FormPage.objects.all()”来查询formfields。我有一个 FormPage (AbstractEmailForm) 和 FormField(AbstractFormField),我希望可见的表单字段出现在我的 HomePage(Page) 上。