【问题标题】:Django CMS Plugin Development: __init__() got an unexpected keyword argument 'instance'Django CMS 插件开发:__init__() 有一个意外的关键字参数“实例”
【发布时间】:2017-07-01 10:04:47
【问题描述】:

我正在创建 Django CMS 插件。到目前为止,我已经创建了一个表单、模型和插件文件。我想保存 SETTINGS 信息,但是当我去保存它时,它会出现如上所述的错误。以下是详细信息。

cms_plugins.py

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from cms.models import CMSPlugin

from src.survey.forms import SurveyForm
from . import models


class SurveyPluginPublisher(CMSPluginBase):
    """Show Polls entered by Admin."""

    cache = False
    form = SurveyForm
    model = models.SurveyPluginModel  # Must add to show stuff.
    module = "Survey"
    name = "Awesome Survey v1.0"
    render_template = 'survey/_hello.html'


plugin_pool.register_plugin(SurveyPluginPublisher)

forms.py

from django import forms
from src.survey.models import Survey

models.py

class SurveyForm(forms.Form):
    # all_surveys = Survey.objects.only('name')
    # surveys = forms.ModelChoiceField(queryset=all_surveys)
    headline = forms.CharField(max_length=255, help_text='Enter Headline')
    description = forms.CharField(widget=forms.Textarea(attrs={'rows': 5, 'cols': 100}),help_text='Enter Description')

models.py

class SurveyPluginModel(CMSPlugin):
    name = models.CharField("Survey Name", max_length=255, default='Survey Name',
                            help_text='Enter Survey Name')
    description = models.CharField("Survey Description", max_length=500, blank=True, help_text='Write Description here')

    def __str__(self):
        return "Returning some Survey Text"

【问题讨论】:

  • 你从哪里得到这个错误?显示完整的回溯。

标签: django plugins django-cms


【解决方案1】:

SurveyForm 应该是ModelForm 的子类

class SurveyForm(forms.ModelForm):

    class Meta:
        model = SurveyPluginModel

【讨论】:

  • django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form SurveyForm needs updating.
  • fields = '__all__' 将其整理出来。谢谢
猜你喜欢
  • 2015-01-27
  • 2019-10-07
  • 2021-10-29
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 2014-09-14
相关资源
最近更新 更多