python - 如何将 formbuilder 制作的表单添加到 Wagtail 中的每个页面?

标签 python django django-forms wagtail

有没有办法在 CMS 的每个页面上添加表单(例如反馈表)?我真的很喜欢使用 Wagtail FormBuilder,这样编辑人员就可以更改字段。

我的第一个想法是创建自定义表单页面(继承自 AbstractEmailForm)作为站点根子项,并通过模板标记将其加载到 base.html。我可以通过这种方式访问​​页面属性,但无法呈现表单。

这是我的模板标签:

@register.assignment_tag(takes_context=True)
def get_feedback_form(context):
  return context['request'].site.root_page.get_children().type(FeedbackFormPage).first()

这就是我在 base.html 中使用它的方式:

{% get_feedback_form as feedback_form %}
...
{{ feedback_form.specific.title }} <-- this works
{{ feedback_form.specific.form.as_p }} <-- this doesnt work

以某种方式创建一个表单作为片段或将其添加到网站设置会很好,但我没有找到如何做到这一点。

最佳答案

主要问题是如何使用 .form.as_p 在模板中生成表单。

您需要使用 .get_form 函数生成表单,但最好在模板中执行此操作,因为当前用户和页面需要像这样作为参数传入。

        form = feedback_form_page.get_form(
        page=feedback_form_page, user=request.user)

您可以在此处查看如何为 AbstractForm 模型构建表单: https://github.com/wagtail/wagtail/blob/master/wagtail/wagtailforms/models.py#L278

下面是完整的详细示例,以及如何将表单选择工作到网站设置模块中。

链接到站点设置中的表单

假设您指的是站点设置贡献模块: http://docs.wagtail.io/en/v1.13/reference/contrib/settings.html

文档的“编辑处理程序”部分介绍了一种在站点设置中链接到页面的好方法。 http://docs.wagtail.io/en/v1.13/reference/contrib/settings.html?highlight=site%20settings#edit-handlers

示例(在 models.py 中):

from wagtail.contrib.settings.models import BaseSetting, register_setting
# ...
@register_setting
class MyCustomSettings(BaseSetting):
    feedback_form_page = models.ForeignKey(
        'wagtailcore.Page', null=True, on_delete=models.SET_NULL)

    panels = [
        # note the page type declared within the pagechooserpanel
        PageChooserPanel('feedback_form_page', ['base.FormPage']),
    ]

设置此模型后,您需要执行 makemigrationmigrate 以使更改在管理中生效。然后您将在设置菜单中看到一个名为“我的自定义设置”的子菜单

将链接的表单添加到每个页面

添加一个包含在您的基本模板中的 block (以便它可以在模板中被覆盖)(例如 myapp/templates/base.html)。

<!-- Footer -->
<footer>
    {% block feedback_form %}{% include "includes/feedback_form.html" %}{% endblock feedback_form %}
    {% include "includes/footer.html" %}
</footer>

创建一个包含模板(例如 myapp/templates/includes/feedback_form.html)

{% load feedback_form_tags wagtailcore_tags %}

{% get_feedback_form as feedback_form %}

<form action="{% pageurl feedback_form.page %}" method="POST" role="form">
  <h3>{{ feedback_form.page.title}}</h3>
  {% csrf_token %}
  {{ feedback_form.form.as_p }}
  <input type="submit">
</form>

构建模板标签获取表单和页面

您的模板标签需要使用页面的 self.get_form() 函数构建表单。例如。你你的模板标签 (base/templatetags/feedback_form)

from django import template
from myapp.models import MyCustomSettings

register = template.Library()
# https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/


@register.assignment_tag(takes_context=True)
def get_feedback_form(context):
    request = context['request']
    my_custom_settings = MyCustomSettings.for_site(request.site)
    feedback_form_page = my_custom_settings.feedback_form_page.specific
    form = feedback_form_page.get_form(
        page=feedback_form_page, user=request.user)
    return {'page': feedback_form_page, 'form': form}

关于python - 如何将 formbuilder 制作的表单添加到 Wagtail 中的每个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42673180/

相关文章:

python - 使用 python 从图像集中提取 hog 特征时出错

python - 如何将数据写入窗口opencv

python - 在没有 jasperserver 的情况下从 python 中运行 jasper 报告(使用 iReport 创建)?

python - 测试 ManyToManyField 的最佳方法

python - Django 休息框架 : Get detail view using a field other than primary key integer id

django - 如何使用小部件属性而不是 css 更改 django 表单中下拉字段的宽度

python - Django:如何让表单向导在创建表单时接收请求对象?

python - 编辑 scikit-learn 决策树

Django-admin clean_fields 覆盖,在提交时保留以前的数据

python - Django - 在 'INSTALLED APPS' 中添加应用程序名称时出现值错误