46

一些细节:

Request Method: GET
Request URL: http://localhost:8080/user/create
Django Version: 1.5.1
Exception Type: TypeError
Exception Value: ____init____() got an unexpected keyword argument 'instance'
Exception Location: /place/venv/local/lib/python2.7/site-packages/django/views/generic/edit.py in get_form, line 35
Python Executable: /place/venv/bin/python
Python Version: 2.7.3

视图.py

class UserCreateView(CreateView):
    model = models.User
    form_class = forms.UserForm

网址.py

url(r'^user/create$', UserCreateView.as_view(), name='user_create'),

表格.py

class UserForm(forms.Form):
    GROUP_CHOICES = [(-1, '[Choose]')]
    GROUP_CHOICES += [(group.id, group.name.capitalize()) for group in auth.models.Group.objects.all()]

    email = forms.EmailField(
        label='Email',
        widget=forms.TextInput(attrs={'placeholder': 'Email'})
    )
    first_name = forms.CharField(
        label='First Name',
        widget=forms.TextInput(attrs={'placeholder': 'First Name'})
    )
    last_name = forms.CharField(
        label='Last Name',
        widget=forms.TextInput(attrs={'placeholder': 'Last Name'})
    )
    password = forms.CharField(
        label='Password',
        widget=forms.PasswordInput(attrs={'placeholder': 'Password'})
    )
    password_validation = forms.CharField(
        label='Repeat Password',
        widget=forms.PasswordInput(attrs={'placeholder': 'Repeat Password'})
    )
    mobile_number = forms.CharField(
        label='Mobile Number',
        widget=forms.TextInput(attrs={'placeholder': 'Mobile Number'})
    )
    office_number = forms.CharField(
        label='Office Number',
        widget=forms.TextInput(attrs={'placeholder': 'Office Number'})
    )
    group = forms.ChoiceField(
        label='Group',
        choices=GROUP_CHOICES
    )

    def clean_password_validation(self):
        if self.cleaned_data['password'] == self.cleaned_data['password_validation']:
            return self.cleaned_data['password_validation']
        else:
            raise forms.ValidationError('Passwords don\'t match')

    def clean_group(self):
        if self.cleaned_data['group'] != -1:
            return self.cleaned_data['group']
        else:
            raise forms.ValidationError('Please, choose a group')

模型.py

class User(models.Model):
    user = models.OneToOneField(auth.models.User)
    mobile_number = models.CharField(max_length=64)
    office_number = models.CharField(max_length=64)
4

4 回答 4

92

我怀疑类UserForm应该是模型形式。您可能想要更改字段,但它应该从 `ModelForm.

所以将表单定义更改为

class UserForm(forms.ModelForm):
   class Meta:
       model = User
       fields = [...] # list of fields you want from model

   #or define fields that you want.
   ....
于 2013-04-18T09:42:41.337 回答
2

我错误地覆盖了 __init__() 方法,没有初始参数,如下所示

class MyForm(forms.ModelForm):
    ...
    def __init__(self):
        super(CaseForm, self).__init__()
        ...

结果得到这个错误

/case/create 处的类型错误

__init__() 得到了一个意外的关键字参数“initial”

为了修复它,我将参数设置为 __init__() 并在调用超类 __init__() 时传递它们,请参见下面的结果

class MyForm(forms.ModelForm):
    ...
    def __init__(self, *args, **kwargs):
        super(CaseForm, self).__init__(*args, **kwargs)
        ...
于 2019-11-04T15:22:58.993 回答
1

forms.py 在方括号中定义字段,例如 fields=['field 1', 'field 2',...]

class CustomerForm(forms.ModelForm):        
    class Meta:
        model = Customer
        fields = ['fname','lname','email','address','city','state','zip','username','password','age','mobile','phone']
于 2016-10-12T12:15:32.550 回答
1

我解决了尝试将表单保存到数据库时遇到的类似错误。“Report() 得到了一个意外的关键字参数‘摘要’”

问题是 models.py 和 forms.py 中的字段名称不匹配。

在models.py 的类Report 中,该字段的名称是“summary_input”,但在forms.py 中它被命名为“summary”,所以我更改了forms 中的变量名以匹配models 中的变量名。

# models.py
class Report(models.Model):
    summary_input = models.TextField()

# forms.py
class ReportForm(forms.Form):
    summary = forms.CharField(widget=forms.widgets.Textarea)
    # changed to 
    # summary_input = forms.CharField(widget=forms.widgets.Textarea)
于 2020-05-08T16:35:18.280 回答