我想創建一個用於重置用戶密碼的表單。它應該採取current_password
,然後new_password
和confirm_new_password
。我可以通過驗證來檢查新密碼是否匹配。我將如何驗證current_password
?有沒有辦法將User
對象傳遞給表單?用於重置密碼的Django +表單
5
A
回答
6
Django帶有一個內置的PasswordChangeForm
您可以導入並在您的視圖中使用。
from django.contrib.auth.forms import PasswordChangeForm
但是,你甚至不必編寫自己的密碼重置視圖。有一對視圖django.contrib.with.views.password_change
和django.contrib.auth.views.password_change_done
,你可以直接進入你的URL配置。
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')
相關問題
- 1. Codeigniter - 用於重置密碼的表單
- 2. Django密碼重置表
- 3. Django 1.11 - 密碼重置
- 4. 如何重置密碼Django?
- 5. 在Django中重置密碼
- 6. Django的密碼重置應用
- 7. Django的驗證()重置密碼
- 8. Django NoReverseMatch在/ accounts/password/reset /用於密碼重置
- 9. django中的多個重置密碼
- 10. Django的密碼重置投擲「password_reset_done」
- 11. Django密碼重置 - DoesNotExist例外
- 12. django密碼重置NoReverseMatch錯誤
- 13. Django密碼重置。不發送郵件
- 14. SMTPSenderReadsed在Django 1.7中,當重置密碼
- 15. 如何重置Django管理員密碼?
- 16. Webmatrix重置密碼
- 17. 帶密碼的密碼重置laravel 4.2
- 18. 使用實例重置表單Django
- 19. 重置表單身份驗證用戶密碼asp.net C#
- 20. Drupal重置用戶密碼
- 21. 重置用戶密碼
- 22. 重置postgres用戶密碼?
- 23. 用PHP重置LDAP密碼
- 24. 重置加密密碼
- 25. ASP.Net密碼重置時的密碼重置爲「無效令牌」,密碼爲
- 26. 的Django 1.0,使用缺省密碼重置
- 27. Django:反向不解析內置密碼重置的url
- 28. FOSUserBundle重置密碼不適用於模擬用戶配置
- 29. 重置PBKDF2中的密碼
- 30. PHP密碼重置
對我而言,這是行不通的。由於我在包含許多其他事物的表單中組合了密碼重置。但這將是這種用例的正確方法。 – KVISH 2012-07-27 01:31:24
@KVISH我知道這是一個非常晚的評論,但爲了記錄,您可以在HTML'