2016-07-09 22 views
0

Ich versuche, einen Code für das Registrierungsformular aus meinem älteren Projekt zu verwenden. Das Problem ist, dass Django sagt, dass UserCreationForm kein Attribut clean_password1 hat.'Super' Objekt hat kein Attribut 'clean_password1'

Wissen Sie, wo das Problem liegt? Ich sehe, dass es kein solches Attribut in UserCreationForm gibt, aber es hat vorher funktioniert.

Was soll ich tun, damit es funktioniert?

EDIT: Es ist, weil es super (...). Clean_password1, die nicht in der Super-Klasse ist. Also habe ich versucht, super (...). Cleaned_data.get ("password1") zu setzen, aber es wird der Fehler angezeigt, dass cleaned_data nicht da ist (in der superclass).

class UserProfileCreationForm(UserCreationForm): 
    username = forms.CharField(label="Username", max_length=40, min_length=5) 
    email = forms.EmailField(label="Email address", max_length=40) 
    first_name = forms.CharField(label='First name', max_length=40, required=False) 
    last_name = forms.CharField(label='Last name', max_length=40, required=False) 

    password1 = forms.CharField(label="Password", widget=forms.PasswordInput, min_length=5) 
    password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput) 

    class Meta(): 
     model = UserProfile 
     fields = (
      'username', 'email', 'type_of_user', 'first_name', 'last_name', 'password1', 'password2','IBAN', 

     ) 

    def clean_password1(self): 
     password = self.cleaned_data.get('password1') 
     if len(password) < 8: 
      raise ValidationError('Password has to be of size at least 8 characters') 
     return super(UserProfileCreationForm, self).clean_password1() 

    def clean_password2(self): 
     password1 = self.cleaned_data.get("password1") 
     password2 = self.cleaned_data.get("password2") 
     if password1 and password2 and password1 != password2: 
      raise forms.ValidationError("Password mismatch") 
     return password2 

    def save(self, commit=True): 
     try: 
      with transaction.atomic(): 
       user = User(username=self.cleaned_data['username'], 
          first_name=self.cleaned_data['first_name'], 
          last_name=self.cleaned_data['last_name'], 
          email=self.cleaned_data['email']) 
       user.save() 
       user.set_password(self.cleaned_data["password1"]) 

       user_profile = UserProfile(user=user, 
              IBAN=self.cleaned_data['IBAN'], 
              type_of_user=self.cleaned_data['type_of_user'] 
              ) 
       user_profile.save() 
     except: 
      raise #TODO: HANDLE THE EXCEPTION 

     return user 

Antwort

0

Sie können sicher entfernen clean_password2 - Django prüft bereits, wenn die Passwörter übereinstimmen (ich nehme an, Sie die aktuelle Version für Ihr neues Projekt verwenden). Wie für clean_password1 würde ich empfehlen, es auch zu entfernen und lesen Sie die Dokumentation password validation (neu in Django 1.9).

0

Es gibt keinen Grund, super innerhalb einer clean_field-Methode aufzurufen; Das Feld existiert nicht in der Basisklasse, also auch nicht im clean_field.