我遇到了Django用戶更改密碼的問題 - 我在Django中建立了幾個生產站點,大約一年(或1.8)都沒有生成,但我不記得有這個問題之前。django用戶在密碼更改後註銷
摘要
當用戶更改他們的密碼,用戶將被註銷,但密碼修改成功。
詳細
我有一個觀點,即允許用戶更改密碼,我使用標準Django表單和權威性框架,強調:更改密碼的作品,它只是記錄了用戶輸出,以便他們必須再次登錄。如果我需要在代碼中重新驗證用戶,那麼我將會看起來有點笨拙。如果我需要在代碼中重新驗證用戶,那麼我將會看起來很笨重。
這裏是我的看法功能:
@login_required
def user_change_password(request):
"""Allows a user to change their password"""
if request.method == "POST":
form = SubscriberPasswordForm(request.POST)
if form.is_valid():
try:
request.user.set_password(form.cleaned_data['password'])
request.user.save()
except Exception, err:
print "Error changing password: {}".format(err)
messages.add_message(request, messages.ERROR, 'The password could not be changed, please try again '
'later. This admins have been notified of this error.')
else:
#this outputs True
print request.user.is_authenticated()
messages.add_message(request, messages.INFO, 'Your password has been changed successfully')
return HttpResponseRedirect("/accounts/dashboard/")
else:
form = SubscriberPasswordForm()
return render(request, "accounts/change-password.html", {"form": form})
所以更改了密碼,用戶被重定向到儀表板頁面中,@login_required裝飾然後重定向他們回到登錄屏幕。
密碼錶單在這裏,雖然它非常簡單。
class SubscriberPasswordForm(forms.Form):
password = forms.CharField(widget=forms.PasswordInput)
cpassword = forms.CharField(widget=forms.PasswordInput)
def clean_cpassword(self):
password1 = self.cleaned_data.get("password")
password2 = self.cleaned_data.get("cpassword")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
code='password_mismatch',
)
此:
以下字段必須在POST請求中提供是一個去od事情;無論如何,你應該這樣做。它在1.7 –