配置不正确的表单 - Django

时间:2015-12-21 11:01:23

标签: python django django-models django-forms django-views

最近,我已经弄清楚如何在Django中创建注册。问题是只有三个字段 - usernamepassword并且它是确认。

在我的项目中,至少有两种类型users具有不同的属性。 CustomersFreelancers。所以我试图在StackOverflow上找到新模型,但是它返回Exception:

Exception Type: ImproperlyConfigured
Exception Value:    
Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form UserCreateForm needs updating.

MODELS.PY:

class UserCustomerProfile(models.Model):
    user = models.OneToOneField(User)
    email = models.EmailField()
    age = models.IntegerField()

    def __str__(self):
          return "%s's profile" % self.user

FORMS.PY

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User

    def save(self, commit=True):
        if not commit:
            raise NotImplementedError("Can't create User and UserProfile without database save")
        user = super(UserCreateForm, self).save(commit=True)
        user_profile = UserCustomerProfile(user=user, email=self.cleaned_data['email'])
        user_profile.save()
        return user, user_profile

VIEWS.PY:

def register(request):
     if request.method == 'POST':
         form = UserCustomerProfile(request.POST)
         if form.is_valid():
             form.save()
             return HttpResponseRedirect('register/complete/')

     else:
         form = UserCreateForm()
     token = {}
     token.update(csrf(request))
     token['form'] = form

     return render_to_response('register/registration_form.html', token)

你知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

  

禁止创建没有'fields'属性或'exclude'属性的ModelForm;表单UserCreateForm需要更新。

尝试例如:

$(this).parent().next('.menudrop')