2017-11-28 28 views
1

私はdjango-custom-userを使って電子メールでログインしています。私は2つのモデルCustomUserとClientDataていますDjangoクリーンパスワード 'ValidationError'オブジェクトに 'get'属性がありません

models.py

class CustomUser(AbstractEmailUser): 
    first_name = models.CharField(max_length=200, blank=True, null=True, default=None) 
    last_name = models.CharField(max_length=200, blank=True, null=True, default=None) 
    phone = models.CharField(max_length=20, blank=True, null=True, default=None) 

class ClientData(models.Model): 
    user = models.OneToOneField(CustomUser) 
    company = models.CharField(max_length=200, blank=True, null=True, default=None) 
    bank_account = models.CharField(max_length=25, blank=True, null=True, default=None) 

私は両方のモデルで登録フォームを作成しようとしていますし、私はそれを行うために管理している、私もきれいなパスワードを行いました機能、すべてが動作しますが、私は意図的に2つの異なるパスワードを与えたときに私が手:

'ValidationError' object has no attribute 'get' 

forms.py

class UserRegistrationForm(forms.ModelForm): 
    email = forms.EmailField(required=True, max_length=150) 
    first_name = forms.CharField(required=True, max_length=100) 
    last_name = forms.CharField(required=True, max_length=100) 
    password1 = forms.CharField(required=True, label='Password', max_length=100, widget=forms.PasswordInput()) 
    password2 = forms.CharField(required=True, label='Confirm Password', max_length=100, widget=forms.PasswordInput()) 
    phone = forms.CharField(required=True, max_length=20) 

    class Meta: 
     model = CustomUser 
     fields = ['email', 'first_name', 'last_name', 'password1', 'password2', 'phone'] 

    def clean(self): 
     cleaned_data = super(UserRegistrationForm, self).clean() 
     password1 = cleaned_data.get('password1') 
     password2 = cleaned_data.get('password2') 
     if password1 != password2: 
      return forms.ValidationError('Password did not match.') 
     return cleaned_data 

class UserDataRegistrationForm(forms.ModelForm): 

    class Meta: 
     model = ClientData 
     fields = ['company', 'bank_account'] 

、これは私が作った図である。

views.py

def register(request): 
    data = dict() 
    if request.method == 'POST': 
     user_form = UserRegistrationForm(request.POST) 
     data_form = UserDataRegistrationForm(request.POST) 
     if user_form.is_valid() * data_form.is_valid(): 
      cd_user = user_form.cleaned_data 
      cd_data = data_form.cleaned_data 
      first_name = cd_user['first_name'] 
      last_name = cd_user['last_name'] 
      email = cd_user['email'] 
      password = cd_user['password1'] 
      phone = cd_user['phone'] 
      company = cd_data['company'] 
      bank_account = cd_data['bank_account'] 
      new_user = CustomUser.objects.create_user(email, password) 
      ClientData.objects.create(user=new_user, company=company, bank_account=bank_account) 
      new_user.first_name = first_name 
      new_user.last_name = last_name 
      new_user.phone = phone 
      new_user.company = company 
      new_user.bank_account = bank_account 
      new_user.save() 
    else: 
     user_form = UserRegistrationForm() 
     data_form = UserDataRegistrationForm() 
    data['user_form'] = user_form 
    data['data_form'] = data_form 
    return render(request, 'registration/register.html', data) 

は、なぜ私はこのエラーを取得していますか?

答えて

4

検証エラーは返さないでください。

if password1 != password2: 
    raise forms.ValidationError('Password did not match.') 
+0

ありがとうございました!それが問題だった –

関連する問題