如何在Django CMS中将Django应用程序与多个表单集成为插件?

时间:2017-07-31 08:46:13

标签: python django django-forms django-cms

我按照tutorial将我的Django应用程序集成到我的Django CMS网站中。

我的应用促销有两个模型void onCreate (SQLiteDatabase db) OrderModel(),它们都在CustomerModel()中用于将某些字段传递给表单,如下所示:

forms.py

如教程中所述,在class CustomerForm(forms.ModelForm): class Meta: model = CustomerModel fields = ['firstname', 'surname', 'company', 'email',] class OrderForm(forms.ModelForm): class Meta: model = OrderModel fields = ['product', 'delivery_date',] 我生成models.py并在SalePluginModel中,我指定了cms_plugins.py插件类,该类负责为django CMS提供渲染我的应用所需的信息,如SalePluginPublisher

中所示
models.py

class SalePluginModel(CMSPlugin): title = models.CharField(u'title', blank=True, help_text=u'Optional. Title of the widget.', max_length=64, )

cms_plugins.py

现在,问题是,只有一个表单类可以作为属性传递给class SalePluginPublisher(CMSPluginBase): model = SalePluginModel # model where plugin data are saved module = _("Sale") name = _("Sale Plugin") # name of the plugin in the interface render_template = "sale/sale_plugin.html" form = '' # How to pass multiple model forms here? 。是否有可能在CMSPluginBase或一般情况下包含两个表单类,如何将我的应用程序与Django CMS中的两个模型和表单集成? 非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

所以要澄清一下,你想将两个表单传递给插件吗?如果是这样,您可以执行以下操作:

class SalePluginPublisher(CMSPluginBase):
    model = SalePluginModel  # model where plugin data are saved
    module = _("Sale")
    name = _("Sale Plugin")  # name of the plugin in the interface
    render_template = "sale/sale_plugin.html"

    def render(self, context, instance, placeholder):
        context = super().render(context, instance, placeholder)
        context['customer_form'] = CustomerForm()
        context['order_form'] = OrderForm()
        return context

如果您没有专用的POST网址,也可以在render功能中处理POST响应。您只需要访问请求对象,例如self.request.POST

编辑:

以上适用于Python 3,要与Python 2一起使用,您需要将render函数的第一行更改为:

context = super(SalePluginPublisher, self).render(context, instance, placeholder)
...