获取__init __()得到一个意想不到的关键字参数“实例”与Django的CreateView [英] Getting __init__() got an unexpected keyword argument 'instance' with CreateView of Django

查看:119
本文介绍了获取__init __()得到一个意想不到的关键字参数“实例”与Django的CreateView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



views.py



views.py

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



urls.py



urls.py

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



forms.py



forms.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')



models.py



models.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)


推荐答案

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

I suspect class UserForm should be model form. You may want to change fields, but it should be derived from `ModelForm.

所以将表单定义更改为

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

   #or define fields that you want.
   ....

这篇关于获取__init __()得到一个意想不到的关键字参数“实例”与Django的CreateView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆