Tôi muốn tạo biểu mẫu để đặt lại mật khẩu của người dùng. Cần lấy số current_password
và sau đó là new_password
và confirm_new_password
. Tôi có thể thực hiện xác nhận để kiểm tra mật khẩu mới phù hợp. Làm cách nào để tôi có thể xác thực current_password
? Có cách nào để vượt qua đối tượng User
vào biểu mẫu không?Biểu mẫu Django + để đặt lại mật khẩu
5
A
Trả lời
6
Django có kèm theo PasswordChangeForm
mà bạn có thể nhập và sử dụng trong chế độ xem của mình.
from django.contrib.auth.forms import PasswordChangeForm
Nhưng bạn thậm chí không phải viết chế độ xem đặt lại mật khẩu của riêng mình. Có một cặp lượt xem django.contrib.with.views.password_change
và django.contrib.auth.views.password_change_done
, bạn có thể móc thẳng vào cấu hình URL của mình.
0
Tìm thấy một ví dụ thực sự tốt về điều này: http://djangosnippets.org/snippets/158/
[EDIT]
tôi đã sử dụng các liên kết ở trên và thực hiện một vài thay đổi. Họ đang ở đây bên dưới:
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')
Các vấn đề liên quan
- 1. Tạo mẫu thư để đặt lại mật khẩu
- 2. Chủ đề email đặt lại mật khẩu Django
- 3. Tạo mã bí mật để đặt lại mật khẩu
- 4. Devise - Recoverable (Đặt lại mật khẩu)
- 5. Cách an toàn để đặt lại mật khẩu hoặc cung cấp mật khẩu cũ
- 6. Đặt lại mật khẩu ASP.NET - vấn đề bảo mật?
- 7. Thiết lập lại mật khẩu đăng ký django tuỳ chỉnh mẫu không tải
- 8. Mã hóa mật khẩu trên POST Django
- 9. Biểu mẫu Django - đặt nhãn
- 10. Đặt lại mật khẩu gốc MySQL thành "mặc định" - MAMP
- 11. Django vấn đề mật khẩu
- 12. Trình tạo mật khẩu Django
- 13. Tôi quên mật khẩu chính Glassfish3, làm cách nào tôi có thể đặt lại mật khẩu?
- 14. Đặt lại mật khẩu bằng cách gửi email mật khẩu tạm thời
- 15. Xác thực biểu mẫu Javascript với mật khẩu xác nhận
- 16. Đặt lại mật khẩu gốc của MySQL khi mật khẩu hiện tại không được biết
- 17. AngularJs. $ SetPristine để đặt lại biểu mẫu
- 18. Đăng ký người dùng Django - Đặt lại mật khẩu qua email
- 19. Dữ liệu tải lại biểu mẫu Django
- 20. Thực hành tốt nhất để đặt lại mật khẩu người dùng bị lãng quên
- 21. django xác nhận mật khẩu validator
- 22. Cách khôi phục hoặc đặt lại mật khẩu gói SSIS?
- 23. đặt lại mật khẩu gốc với cấu hình sai mysql
- 24. Đặt lại trạng thái khôi phục mật khẩu
- 25. .NET C# Đặt lại Mật khẩu - Ngẫu nhiên!
- 26. Magento: Liên kết đặt lại mật khẩu không hoạt động
- 27. Cách tạo mật khẩu ngẫu nhiên hoặc URL tạm thời để đặt lại mật khẩu trong Zend Framework?
- 28. Thay đổi mật khẩu trong Django quản
- 29. Quên mật khẩu Mẫu hoặc kỹ thuật? ¿Tên mẫu?
- 30. Xác minh mật khẩu Django trong Ruby on Rails cung cấp mật khẩu không khớp
Vì mục đích của tôi, thao tác này sẽ không hoạt động. Vì tôi đang kết hợp đặt lại mật khẩu trong một biểu mẫu bao gồm nhiều thứ khác. Nhưng đây sẽ là cách chính xác cho trường hợp sử dụng này. – KVISH
@KVISH Tôi biết đây là một nhận xét rất muộn nhưng đối với hồ sơ, bạn có thể hiển thị và xử lý nhiều hơn một biểu mẫu Django bên trong một HTML '