2012-07-27 5 views
5

사용자 비밀번호 재설정을위한 양식을 만들고 싶었습니다. current_password, 그 다음에 new_passwordconfirm_new_password이 있어야합니다. 새 암호가 일치하는지 확인하기 위해 유효성 검사를 수행 할 수 있습니다. current_password의 유효성을 어떻게 확인할 수 있습니까? User 개체를 양식에 전달할 수 있습니까?Django + 비밀번호 재설정 양식

답변

6

장고는 당신이 가져보기에 사용할 수 PasswordChangeForm 내장되어 있습니다.

from django.contrib.auth.forms import PasswordChangeForm 

하지만 비밀번호 재설정보기를 직접 작성하지 않아도됩니다. 뷰 django.contrib.with.views.password_changedjango.contrib.auth.views.password_change_done이 있으며 URL 구성에 바로 연결할 수 있습니다.

+0

제 목적으로는 작동하지 않습니다. 이후 나는 많은 다른 것들을 포함하는 형태로 비밀 번호 재설정을 결합 해요. 그러나 이것이이 사용 사례를위한 올바른 방법 일 것입니다. – KVISH

+0

@KVISH 매우 늦은 주석이지만 기록을 위해, HTML'

'안에 하나 이상의 Django Form을 표시하고 처리 할 수 ​​있습니다. 다른 변경 사항을 위해 다른 양식과 함께'PasswordChangeForm'을 사용할 수없는 몇 가지 이유가 있습니다. – Oli

0

좋은 예를 발견 : [편집]

내가 위의 링크를 사용하고 몇 가지 변경을

http://djangosnippets.org/snippets/158/. 그들은 여기에 다음과 같습니다 :

class PasswordForm(forms.Form): 
    password = forms.CharField(widget=forms.PasswordInput, required=False) 
    confirm_password = forms.CharField(widget=forms.PasswordInput, required=False) 
    current_password = forms.CharField(widget=forms.PasswordInput, required=False) 

    def __init__(self, user, *args, **kwargs): 
     self.user = user 
     super(PasswordForm, self).__init__(*args, **kwargs) 

    def clean_current_password(self): 
     # If the user entered the current password, make sure it's right 
     if self.cleaned_data['current_password'] and not self.user.check_password(self.cleaned_data['current_password']): 
      raise ValidationError('This is not your current password. Please try again.') 

     # If the user entered the current password, make sure they entered the new passwords as well 
     if self.cleaned_data['current_password'] and not (self.cleaned_data['password'] or self.cleaned_data['confirm_password']): 
      raise ValidationError('Please enter a new password and a confirmation to update.') 

     return self.cleaned_data['current_password'] 

    def clean_confirm_password(self): 
     # Make sure the new password and confirmation match 
     password1 = self.cleaned_data.get('password') 
     password2 = self.cleaned_data.get('confirm_password') 

     if password1 != password2: 
      raise forms.ValidationError("Your passwords didn't match. Please try again.") 

     return self.cleaned_data.get('confirm_password') 
관련 문제