2016-05-07 18 views
0

Also möchte ich meinen Benutzer sein Profil bearbeiten lassen.Django, Benutzerprofil mit Passwortbestätigung aktualisieren

Benutzer hat Benutzername, E-Mail und Bild.

Zusätzlich möchte ich Benutzer sein Passwort bestätigen, wenn er sein Profil bearbeitet und das ist, wo mein Problem ist.

Es sieht aus wie die

enter image description here

Statt mein Passwort und Benutzernamen Update der Überprüfung und/oder E-Mail und/oder Bild, mein Programm alles aktualisiert. Plus-Passwort ist nach dem Update gebrochen.

forms.py

class UpdateProfile(forms.ModelForm): 
    username = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), 
           label=_("Username"), error_messages={ 
      'invalid': _("This value must contain only letters, numbers and underscores.")}) 
    email = forms.EmailField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Email address")) 
    password = forms.CharField(
     widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password")) 
    picture = forms.ImageField(required=False) 

    class Meta: 
     model = Person 
     fields = ('username', 'email', 'password', 'picture') 

    def clean_username(self): 
     if self.initial['username'] == self.cleaned_data['username']: 
      return self.cleaned_data['username'] 
     try: 
      user = Person.objects.get(username__iexact=self.cleaned_data['username']) 
     except Person.DoesNotExist: 
      return self.cleaned_data['username'] 
     raise forms.ValidationError(_("The username already exists. Please try another one.")) 

    def clean_email(self): 
     email = self.cleaned_data["email"] 
     if self.initial['email'] == email: 
      return email 
     try: 
      gg = Person.objects.get(email=email) 
     except Person.DoesNotExist: 
      return email 
     print("DUPLICATE") 
     raise forms.ValidationError('duplicate email') 

    def clean(self): 
     if 'password' in self.cleaned_data : 
      p =Person.objects.get(username__iexact=self.initial['username']) 
      if not p.check_password(self.cleaned_data['password']): 
       raise forms.ValidationError(_("Password is wrong.")) 
     return self.cleaned_data 

views.py

def edit(request): 
    args = {} 
    args.update(csrf(request)) 
    if request.method == 'POST': 
     user_form = UpdateProfile(request.POST, request.FILES, instance=Person.objects.get_by_natural_key(request.user.get_username())) 
     args['user_form'] = user_form 
     if user_form.is_valid(): 
      user_form.save() 
      return HttpResponseRedirect('/profile/') 
    args['user_form'] = UpdateProfile(request.POST or None) 
    args['user2'] = Person.objects.get_by_natural_key(request.user.get_username()) 
    return render_to_response('edit.html', args, context_instance=RequestContext(request)) 

In html habe ich Standardformular.

Ich denke, dass das Problem ist, dass ich Passwort-Feld in UpdateProfile habe. Ich weiß jedoch nicht, wie ich es zum Laufen bringen kann.

Antwort

0
class Meta: 
     model = Person 
     fields = ('username', 'email', 'picture') 

Ich löschte Passwort aus Feldern und jetzt funktioniert es. Ich weiß nicht, ob es eine richtige Lösung meines Problems ist, aber es funktioniert.

Verwandte Themen