2013-04-18 17 views
23

kilka szczegółów:

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

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

urls.py

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

formy .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

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

Odpowiedz

48

Podejrzewam klasę UserForm powinien być wzór formularza. Możesz zmienić pola, ale powinny one pochodzić z `ModelForm.

więc zmienić definicję formularz do

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

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

Czy nie można utworzyć formularza bez modeli w CreateView? – tilaprimera

+1

@tilaprimera: Nie sądzę, że jest to możliwe w 'CreateView', ale możesz użyć' FormView' i ręcznie nadpisać 'form_valid()'. – SaeX

1

forms.py definiuje Fields w nawiasach kwadratowych takich dziedzinach = [ 'field 1', '2 Pole', ...]

class CustomerForm(forms.ModelForm):   
    class Meta: 
     model = Customer 
     fields = ['fname','lname','email','address','city','state','zip','username','password','age','mobile','phone'] 
Powiązane problemy