为django CMS提供的简单插件表单
项目描述
本软件包提供了一种处理django-CMS插件表单提交的机制。
背景 & 方法
背景
插件是django CMS的一个关键组件,用于在django CMS项目中创建可重用、可配置的内容片段。由于它们的灵活性和实用性,项目开发人员将受益于通过插件发出表单和处理表单提交。
由于CMS插件是页面的片段,它们不提供用于接收和处理表单提交的唯一URL。这在对表单提交进行处理时提出了许多挑战。
方法
为了克服这些限制,本软件包采用的方法是将来自子类FormPluginBase的插件表单提交定向到django CMS URL空间之外的URL,并由本软件包提供的ProcessFormView处理。
ProcessFormView接受表单提交,对其进行处理,如果有效,则将生成的表单发送回插件类进行处理,然后通过重定向到插件提供的success_url响应请求。
在验证错误的情况下,视图将请求重定向回原始页面,并通过会话变量将表单数据返回给插件表单。
用户体验完全符合预期,表单的处理没有出现“抛出HTTPRedirectResponses”或任何特殊中间件。
此包封装了所有额外逻辑,因此插件开发者只需继承FormPluginBase,而不是通常的cms.plugin_base.CMSPluginBase。
在CMS插件中展示的Form或ModelForm也应包含“混合”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的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1331add5e0d221da0fdacffab822b0d3bdc6ec9016344e7a4d793be0cc5e62e6 |
|
MD5 | b8d43d4263f39303b489027de926032f |
|
BLAKE2b-256 | 312034f457eda82dc370cd9e8aa40790d25c353194e994435ea5f1079f5b0014 |
cmsplugin_form_handler-0.2.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8edd4f2c9f2a3a53b8aab16db5a523e451e8f87e534c5603e3fb10f281746a2f |
|
MD5 | c3308a17e0129e40aef313bd66cbedd0 |
|
BLAKE2b-256 | c6885ba0493a3cb8bb7c0b57a5cd074902906a09a17888c10cbb4111820706d7 |