2014-11-24 2 views
1

사용자가 프로필 정보를 편집 할 수있는 페이지를 만들었습니다.장고 업데이트 암호가 작동하지 않습니다.

사용자가 암호를 변경하면 데이터베이스를 업데이트하지 않고 암호가 손상됩니다.

edit_user.html

<form style="display:inline"class="form-signin" action="/edit_user/" method="post" enctype="multipart/form-data"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <button type="submit" class="btn btn-info"> Save Changes</button> 
</form> 

forms.py

class UserProfileForm(forms.Form): 
    email = forms.EmailField(label='Email', widget=forms.TextInput(attrs={'class' : 'form-control'})) 
    firstname = forms.CharField(label='First Name', max_length=15, widget=forms.TextInput(attrs={'class' : 'form-control'})) 
    lastname = forms.CharField(label='Last Name', max_length=15, widget=forms.TextInput(attrs={'class' : 'form-control'})) 
    #zip = forms.IntegerField(validators=[MinValueValidator(0),MaxValueValidator(99999)], label="Zipcode", widget=forms.TextInput(attrs={'class' : 'form-control'})) 
    oldPassword = forms.CharField(required=False, label='Current Password', widget=forms.PasswordInput(attrs={'class' : 'form-control'})) 
    password1 = forms.CharField(required=False, label="New Password", widget=forms.PasswordInput(attrs={'class' : 'form-control'})) 
    password2 = forms.CharField(required=False, label="Confirm New Password", widget=forms.PasswordInput(attrs={'class' : 'form-control'})) 

    def clean_password(self): 
     if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data: 
      if self.cleaned_data['password1'] != self.cleaned_data['password2']: 
       raise forms.ValidationError(_("The two password fields did not match.")) 
     return self.cleaned_data 

views.py

@login_required 
def edit_user(request): 
    if '_auth_user_id' in request.session: 
     u = User.objects.get(id=request.session['_auth_user_id']) 

     if request.method == 'POST': 
      form = UserProfileForm(request.POST) 
      form.fields["email"].initial = u.email 
      form.fields["firstname"].initial = u.first_name 
      form.fields["lastname"].initial = u.last_name 

      if form.is_valid(): 
       fname = form.cleaned_data['firstname'] 
       lname = form.cleaned_data['lastname'] 
       email = form.cleaned_data['email'] 
       oldPassword = form.cleaned_data['oldPassword'] 
       pword = form.cleaned_data['password1'] 

       # if oldPassword != u.password: 
       #  #"The current password does not match with your old password!" 
       #  return render(request, 'edit_user.html', { 'form': form}) 

       User.objects.filter(id=u.id).update(first_name = fname, last_name = lname, email = email, password = pword) 
       return HttpResponseRedirect('/edit_user/') 

     else: 
      form = UserProfileForm() 
      form.fields["email"].initial = u.email 
      form.fields["firstname"].initial = u.first_name 
      form.fields["lastname"].initial = u.last_name 

     variables = RequestContext(request, {'form': form}) 
     return render(request, 'edit_user.html', variables,) 

내가 관리자 설정으로 이동하여 사용자의 암호를 확인, 그것은이 표시 :

Password: 
Invalid password format or unknown hashing algorithm. 

Raw passwords are not stored, so there is no way to see this user's password, but you can change the password using this form. 

도움이 될 것입니다.

감사합니다.

답변

1

실수로 암호를 일반 텍스트로 저장하는 것을 방지하는 장고입니다.

는 사용자의 암호를 설정하려면 전화 :

user.set_password(new_password) 

이는 correct (hashed) format에 암호를 저장합니다.

django는 이미 a pre-built view for changing passwords을 가지고 있습니다.

u = request.user 
if u.is_authenticated(): 
    ... 
: 직접 요청 떨어져 사용자를 얻을 수 있기 때문에 사이드 노트, 당신이 필요하지 않음을 알고 있어야으로

...

if '_auth_user_id' in request.session: 
    u = User.objects.get(id=request.session['_auth_user_id']) 

...

+0

'u.set_password (pword) '를 추가 한 후에도 "잘못된 비밀번호 형식 또는 알 수없는 해싱 ..."을 계속합니다. – user4234041

+0

'update ='에 대한 호출에서'password = pword'를 제거 했습니까? – meshy

+0

나중에'u.save()'를 호출해야합니다. 이 뷰에서'update' 메소드를 전혀 사용하지 않는 것이 좋습니다. – meshy

관련 문제