我試圖用PasswordInput()以安全的方式確認用戶密碼,但Django似乎只允許每個表單使用一個PasswordInput()。什麼是confirm_password以下實施的最佳方法:如何在Django中確認密碼?
class VerifyAccountsForm(forms.ModelForm):
username = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Username'}), label="")
password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Password'}), label="")
password_verify = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Verify password'}), label="")
def clean_password(self):
print self.cleaned_data
password1 = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password_verify')
if not password2:
raise forms.ValidationError("You must confirm your password")
if password1 != password2:
raise forms.ValidationError(_('Your passwords do not match'), code='invalid')
return password2
clean_data的輸出是唯一的用戶名和密碼,沒有password_verify。
謝謝。是否有可能在不使用模型的情況下實現這一點,但仍然安全地實施它? –
@HC當然,你可以做到 – doniyor