Typerror: __init__() got an unexpected keyword argument 'initial' error. When trying to use class-based views and crispy forms

Typerror: __init__() got an unexpected keyword argument 'initial' error. When trying to use class-based views and crispy forms

这个问题已经困扰我好几天了,我在任何地方都找不到答案,我什至怀疑它实际上是一个错误,但因为我是一个刚开始玩 class 基于观点我想我可能做错了什么。

因此,我使用 LoginRequiredMixin 创建了一个简单的 UpdateView 来更新我的模型并且它运行良好,但是如果我尝试自定义 forms.py 以使用脆皮形式,我会收到错误消息。

这是我的代码:

models.py

class Profile(TimeStampedModel):
    LANGUAGES = (
        ('pt_BR', 'Português(Brasil)'),
        ('en_US', 'English(USA)'),
    )

    user = models.OneToOneField('auth.User')
    language = models.CharField(max_length=255, choices=LANGUAGES, default=LANGUAGES[0][0])

    def get_absolute_url(self):
        return reverse('edit-profile')

forms.py

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('language',)
        labels = {
            'language': 'Idioma',
        }

views.py

class ProfileUpdateView(LoginRequiredMixin, UpdateView):
    form_class = ProfileForm

    template_name = 'accounts/profile.html'

    def get_object(self, queryset=None):
        return self.request.user.profile

到目前为止一切顺利,如果我在我的模板中生成 {{ form.as_p }} 一切正常。

现在,如果我开始在 forms.py 中自定义我的 ProfileForm class,方法是插入:

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('language',)
        labels = {
            'language': 'Idioma',
        }

    def __init__(self):
        super().__init__()

#       self.helper = FormHelper()
#     ... continue crispy form setup...

甚至在我从 crispy 形式实例化任何对象之前,只需插入这两行:

def __init__(self):
    super().__init__()

如果我尝试加载页面,我会收到以下类型错误:

__init__() got an unexpected keyword argument 'initial'

现在,根据我的理解,只需在其中声明 __init__ 和 super() 而无需任何其他参数,class 的行为应该与以前一样,但我得到了回溯:

Traceback (most recent call last):
  File "\myvenv\lib\site-
packages\django\core\handlers\exception.py", line 41, in inner
    response = get_response(request)
  File "\myvenv\lib\site-
packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "\myvenv\lib\site-
packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "\myvenv\lib\site-
packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "\myvenv\lib\site-
packages\django\contrib\auth\mixins.py", line 56, in dispatch
    return super(LoginRequiredMixin, self).dispatch(request, *args, 
**kwargs)
  File "\myvenv\lib\site-
packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "\myvenv\lib\site-
packages\django\views\generic\edit.py", line 236, in get
    return super(BaseUpdateView, self).get(request, *args, **kwargs)
  File "\myvenv\lib\site-
packages\django\views\generic\edit.py", line 174, in get
    return self.render_to_response(self.get_context_data())
  File "\myvenv\lib\site-
packages\django\views\generic\edit.py", line 93, in get_context_data
    kwargs['form'] = self.get_form()
  File "\myvenv\lib\site-
packages\django\views\generic\edit.py", line 45, in get_form
    return form_class(**self.get_form_kwargs())
TypeError: __init__() got an unexpected keyword argument 'initial'

我正在使用 Python 3.6.2 和 Django 1.11.5。

您已经更改了表单的签名,因此它不带任何参数。但是表单需要接受参数——初始值、数据等。您需要确保它继续接受它期望的参数,然后将它们全部传递给 super class 方法。正常的做法是接受 *args, **kwargs.

def __init__(self, *args, **kwargs):
    super().__init__(*args, **kwargs)