鹡鸰不通过自定义字段面板
Wagtail not pulling through custom field panels
我正在通过以下方式覆盖 wagtail AbstractFormField
panel
属性:
...
before_input = RichTextField(verbose_name=_('before input'), blank=True)
after_input = RichTextField(verbose_name=_('after input'), blank=True)
panels = [
FieldPanel('label'),
FieldPanel('before_input'),
FieldPanel('after_input'),
FieldPanel('required'),
FieldPanel('field_type', classname="formbuilder-type"),
FieldPanel('choices', classname="formbuilder-choices"),
FieldPanel('default_value', classname="formbuilder-default"),
]
其他面板是开箱即用的。
这在管理方面工作得很好,而且还以富文本格式保存到我的数据库中
我通过以下方式将其拉到我的模板中的表单中:
<form action="{% pageurl page %}" method="POST" class="lm-ls1" id="feedback-form">
{% csrf_token %}
{{ form.question1.help_text }} <!-- Simpler non interable way -->
{{ form.question1.before_input }}
<p>---------------</p>
{% for row in form.fields.values %}
{{row.choices}}
<p>---------------</p>
{{row.help_text}}
<p>---------------</p>
{{row.before_input}}
{% endfor %}
</form>
但我只得到 html 表单面板的输出,不包括 before_input
和 after_input
我大致经历了以下几点:
Overall, how did you feel about the service you received today?
---------------
[('Very satisfied', 'Very satisfied'), ('Satisfied', 'Satisfied'),
('Neither satisfied nor dissatisfied', 'Neither satisfied nor dissatisfied'), ('Dissatisfied', 'Dissatisfied'), ('Very dissatisfied', 'Very dissatisfied')]
---------------
Overall, how did you feel about the service you received today?
---------------
---------------
如何访问存储在 _formfield
wagtail table 中的 before_input
现场面板数据?
有点晚了,但希望这仍然对您或其他人有帮助。
鹡鸰形式的工作原理
提供给 AbstractFormPage 模型的视图上下文的 Wagtail 表单是一个完全实例化的 Django 表单。这意味着您只能在 form
中找到可以提供给 Django 表单的值。
这包括字段,它们是 Django 的 Fields
(例如 CharField
)的实例,并且没有简单的方法可以向这些字段添加额外的属性。
您可以在 Wagtail FormBuilder
class 定义中看到 Form 对象是如何构建的。
1 - 制作自定义模板标签
在 FormField(Wagtail 的 FormField)上获取附加属性的一种比较简单的方法是使用模板标签。
在您的应用程序的文件夹 templatetags
中创建一个新文件,并构建一个 simple_tag
来获取 form_page 字段(这将是一个 Django Field 实例) 和你想要获取的属性名称的字符串。
# myapp/templatetags/form_tags.py
from django import template
from django.utils.html import mark_safe
register = template.Library()
@register.simple_tag(name='form_field_attribute')
def form_field_attribute(form_page, field, attribute_name, default=None):
"""Return attribute on FormField where field matches 'field' provided."""
# field is a django Field instance
field_name = field.name
results = [
# if html is stored, need to use mark_safe - be careful though.
mark_safe(getattr(form_field, attribute_name, default))
# get_form_fields() is a built in function on AbstractFormPage
for form_field in form_page.get_form_fields()
# clean_name is property on AbstractFormField used for Django Field name
if form_field.clean_name == field_name]
if results:
return results[0]
return default
2 - 修改您的 form_page.html 模板
在您的模板中,循环遍历您的表单(这是 Django 表单实例)并使用模板助手来获取您需要的额外属性。下面的示例,传入 page
或 self
的效果相同,因为它们都是 FormPage
.
的实例
<form action="{% pageurl page %}" method="POST" role="form">
{% csrf_token %}
{% for field in form %}
<div>{% form_field_attribute page field 'before_input' %}</div>
{{ field }}
<div>{% form_field_attribute page field 'after_input' %}</div>
{% endfor %}
<input type="submit">
</form>
我正在通过以下方式覆盖 wagtail AbstractFormField
panel
属性:
...
before_input = RichTextField(verbose_name=_('before input'), blank=True)
after_input = RichTextField(verbose_name=_('after input'), blank=True)
panels = [
FieldPanel('label'),
FieldPanel('before_input'),
FieldPanel('after_input'),
FieldPanel('required'),
FieldPanel('field_type', classname="formbuilder-type"),
FieldPanel('choices', classname="formbuilder-choices"),
FieldPanel('default_value', classname="formbuilder-default"),
]
其他面板是开箱即用的。
这在管理方面工作得很好,而且还以富文本格式保存到我的数据库中
我通过以下方式将其拉到我的模板中的表单中:
<form action="{% pageurl page %}" method="POST" class="lm-ls1" id="feedback-form">
{% csrf_token %}
{{ form.question1.help_text }} <!-- Simpler non interable way -->
{{ form.question1.before_input }}
<p>---------------</p>
{% for row in form.fields.values %}
{{row.choices}}
<p>---------------</p>
{{row.help_text}}
<p>---------------</p>
{{row.before_input}}
{% endfor %}
</form>
但我只得到 html 表单面板的输出,不包括 before_input
和 after_input
我大致经历了以下几点:
Overall, how did you feel about the service you received today?
---------------
[('Very satisfied', 'Very satisfied'), ('Satisfied', 'Satisfied'),
('Neither satisfied nor dissatisfied', 'Neither satisfied nor dissatisfied'), ('Dissatisfied', 'Dissatisfied'), ('Very dissatisfied', 'Very dissatisfied')]
---------------
Overall, how did you feel about the service you received today?
---------------
---------------
如何访问存储在 _formfield
wagtail table 中的 before_input
现场面板数据?
有点晚了,但希望这仍然对您或其他人有帮助。
鹡鸰形式的工作原理
提供给 AbstractFormPage 模型的视图上下文的 Wagtail 表单是一个完全实例化的 Django 表单。这意味着您只能在 form
中找到可以提供给 Django 表单的值。
这包括字段,它们是 Django 的 Fields
(例如 CharField
)的实例,并且没有简单的方法可以向这些字段添加额外的属性。
您可以在 Wagtail FormBuilder
class 定义中看到 Form 对象是如何构建的。
1 - 制作自定义模板标签
在 FormField(Wagtail 的 FormField)上获取附加属性的一种比较简单的方法是使用模板标签。
在您的应用程序的文件夹 templatetags
中创建一个新文件,并构建一个 simple_tag
来获取 form_page 字段(这将是一个 Django Field 实例) 和你想要获取的属性名称的字符串。
# myapp/templatetags/form_tags.py
from django import template
from django.utils.html import mark_safe
register = template.Library()
@register.simple_tag(name='form_field_attribute')
def form_field_attribute(form_page, field, attribute_name, default=None):
"""Return attribute on FormField where field matches 'field' provided."""
# field is a django Field instance
field_name = field.name
results = [
# if html is stored, need to use mark_safe - be careful though.
mark_safe(getattr(form_field, attribute_name, default))
# get_form_fields() is a built in function on AbstractFormPage
for form_field in form_page.get_form_fields()
# clean_name is property on AbstractFormField used for Django Field name
if form_field.clean_name == field_name]
if results:
return results[0]
return default
2 - 修改您的 form_page.html 模板
在您的模板中,循环遍历您的表单(这是 Django 表单实例)并使用模板助手来获取您需要的额外属性。下面的示例,传入 page
或 self
的效果相同,因为它们都是 FormPage
.
<form action="{% pageurl page %}" method="POST" role="form">
{% csrf_token %}
{% for field in form %}
<div>{% form_field_attribute page field 'before_input' %}</div>
{{ field }}
<div>{% form_field_attribute page field 'after_input' %}</div>
{% endfor %}
<input type="submit">
</form>