[英]Password change field reset password django
我使用django通過電子郵件創建了一個密碼重置系統,但是當我進入密碼頁面時,如果我輸入密碼並確認正確,那么它什么都不做,只是重新加載頁面並似乎在表單中出現錯誤。但是即使我填寫了該字段,它仍顯示此字段是必填字段(錯誤)。
此外,您如何做到這一點,因此只能為用戶分配一封電子郵件。 因此,注冊或更改您的信息時,同一電子郵件不能用於兩個帳戶。
這是我的密碼重置頁面的代碼:
{% extends 'base.html' %}
{% block head %}
<link href="\static\accounts\css\forms.css" rel="stylesheet">
<script src="\static\registration\js\emailvariable.js"></script>
{% endblock %}
{% block body %}
{% if validlink %}
<h3 style="text-align: center">Change password</h3>
<form id="login-form" method="post">
{% csrf_token %}
<input placeholder="Password" id="id_password" name="password"
type="password" class="form-control">
<input placeholder="Confirm Password" id="id_password2" name="password2"
type="password" class="form-control">
<div style="text-align:center;">
<button type="submit" class="btn btn-outline-dark centerbutton">Change password</button>
</div>
{% if form.errors %}
<p class=" label label-danger">
<div style="text-align: center">
{{ error | escape }}
</div>
</p>
{% endif %}
</form>
{% else %}
<p>
The password reset link was invalid, possibly because it has already been used.
Please request a new password reset.
</p>
{% endif %}
{% endblock %}
我不確定您的第一個問題,但我可以回答第二個問題。
class SignUpForm(UserCreationForm):
def clean_email(self):
email = self.cleaned_data.get('email')
username = self.cleaned_data.get('username')
if email and User.objects.filter(email=email).exclude(username=username).exists():
raise forms.ValidationError(u'Email addresses must be unique.')
return email
在您擁有注冊表格的forms.py
中放入類似內容。
對於您問題的電子郵件部分:
views.py
email = self.cleaned_data.get('email')
foo = User.objects.filter(email=email)
if foo:
return error
else:
***carry forth with saving the new user***
“ if foo”將查看數據庫中是否存在用戶,並給出了電子郵件地址。 如果是這樣,則返回錯誤。 如果不執行代碼。
我解決了表格問題。
您需要更改:
<input placeholder="Password" id="id_password" name="password" type="password" class="form-control">
<input placeholder="Confirm Password" id="id_password2" name="password2" type="password" class="form-control">
對此:
<input placeholder="Password" name="new_password1" required id="id_new_password1" type="password" class="form-control">
<input placeholder="Confirm Password" name="new_password2" required id="id_new_password2"
type="password" class="form-control">
發生的情況是您的name
輸入錯誤,這通常導致表單在默認的密碼重置視圖中無法通過驗證,並因一般錯誤而返回。
只需將它們換出,表格就可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.