跳转到主要内容

为django CMS提供的简单插件表单

项目描述

PyPI Version Build Status Coverage Status

本软件包提供了一种处理django-CMS插件表单提交的机制。

跳转到下面的快速入门以开始使用,或查看适当的文档

背景 & 方法

背景

插件是django CMS的一个关键组件,用于在django CMS项目中创建可重用、可配置的内容片段。由于它们的灵活性和实用性,项目开发人员将受益于通过插件发出表单和处理表单提交。

由于CMS插件是页面的片段,它们不提供用于接收和处理表单提交的唯一URL。这在对表单提交进行处理时提出了许多挑战。

方法

为了克服这些限制,本软件包采用的方法是将来自子类FormPluginBase的插件表单提交定向到django CMS URL空间之外的URL,并由本软件包提供的ProcessFormView处理。

ProcessFormView接受表单提交,对其进行处理,如果有效,则将生成的表单发送回插件类进行处理,然后通过重定向到插件提供的success_url响应请求。

在验证错误的情况下,视图将请求重定向回原始页面,并通过会话变量将表单数据返回给插件表单。

用户体验完全符合预期,表单的处理没有出现“抛出HTTPRedirectResponses”或任何特殊中间件。

此包封装了所有额外逻辑,因此插件开发者只需继承FormPluginBase,而不是通常的cms.plugin_base.CMSPluginBase

在CMS插件中展示的FormModelForm也应包含“混合”FormPluginFormMixin

快速入门

要快速开始,首先安装此包

pip install cmsplugin-form-handler

将包添加到settings.INSTALLED_APPS

# my_cool_project/settings.py

INSTALLED_APPS = (
    ...
    'cmsplugin_form_handler',
)

在您的URL配置中添加额外的行

urlpatterns = i18n_patterns('',
    url(r'^admin/', include(admin.site.urls)),
    ...
    url(r'^plugin_forms/', include('cmsplugin_form_handler.urls',
                                   namespace='cmsplugin_form_handler')),
    url(r'^', include('cms.urls')),
)

FormPluginFormMixin混合到您的Form

# my_cool_project/forms.py

from django import forms
from cmsplugin_form_handler.forms import FormPluginFormMixin

class MyCoolForm(FormPluginFormMixin, forms.Form):
    # everything else is your normal form.
    my_cool_field = forms.CharField(...)
    ...

或者,如果您使用的是ModelForm

# my_cool_project/forms.py

from django import forms
from cmsplugin_form_handler.forms import FormPluginFormMixin

class MyCoolModelForm(FormPluginFormMixin, forms.ModelForm):
    # everything else is your normal form.
    class Meta:
        model = MyCoolModel
    ...

FormPluginBase派生您的CMS插件

# my_cool_project/cms_plugins.py

from cmsplugin_form_handler.cms_plugins import FormPluginBase

class MyCoolPlugin(FormPluginBase):
    # Use your normal CMSPlugin attributes...
    render_template = 'plugins/my_cool_plugin.html'
    # Note that ``cache = False`` will automatically be set

    # These should be overridden in sub-classes
    form_class = MyCoolForm  # Or, see: get_form_class()
    success_url = '/static/success/url/here'  # Or, see: get_success_url()

    def render(self, context, instance, placeholder):
        context = super(MyCoolPlugin, self).render(context, instance, placeholder)

        # Do your normal thing here
        ...

        return context

    def get_form_class(self, request, instance):
        # Use this method to programmatically determine the form_class.
        # This is what this method does by default:
        return self.form_class

    def get_success_url(self, request, instance):
        # Use this method to programmatically determine the success_url.
        # This is what this method does by default:
        return self.success_url

    def form_valid(self, request, instance, form):
        # Optionally do something with the rendered form here
        # This is what this method does by default:
        form.save()

最后,更新您的插件模板

# my_cool_project/templates/plugins/my_cool_plugin.html

{% load cmsplugin_form_tags %}

<h2>Form Plugin</h2>
<form action="{% cmsplugin_form_action %}" method="post">
    {% csrf_token %}
    {{ cmsplugin_form }}
    <input type="submit">
</form>

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

cmsplugin-form-handler-0.2.0.tar.gz (1.2 MB 查看哈希值)

上传时间

构建分布

cmsplugin_form_handler-0.2.0-py3-none-any.whl (21.4 kB 查看哈希值)

上传时间 Python 3

由以下赞助

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面