Django快速入門10:密碼修改和重置

本章將通過添加密碼更改和重置功能來完成我們的Newspaper應(yīng)用程序的授權(quán)流程。用戶將能夠改變他們當(dāng)前的密碼,或者如果他們忘記了密碼,可以通過電子郵件重新設(shè)置密碼。

首先,我們將實現(xiàn)Django內(nèi)置的視圖和URL,然后用我們自己的Bootstrap驅(qū)動的模板和電子郵件服務(wù)來定制它們。

密碼更改

讓用戶改變密碼是許多網(wǎng)站的常見功能。Django提供個默認(rèn)的實現(xiàn),在登錄的狀態(tài)下訪問http://127.0.0.1:8000/accounts/password_change/即可:

圖片.png

先輸入你的舊密碼,然后再輸入一個新密碼。然后點擊 "更改我的密碼 "按鈕。你將會被轉(zhuǎn)到 "密碼更改成功 "的頁面。

http://127.0.0.1:8000/accounts/password_change/done/

自定義密碼修改頁面

讓我們來定制這兩個密碼修改頁面,讓它們與我們的報紙網(wǎng)站的外觀和感覺相一致。因為Django已經(jīng)為我們創(chuàng)建了視圖和URL,我們只需要改變模板。

templates/registration/password_change_form.html

<!-- templates/registration/password_change_form.html -->
{% extends 'base.html' %}

{% block title %}Password Change{% endblock title %}

{% block content %}
  <h1>Password change</h1>
  <p>Please enter your old password, for security's sake, and then enter
  your new password twice so we can verify you typed it in correctly.</p>

  <form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input class="btn btn-success" type="submit"
      value="Change my password">
  </form>
{% endblock content %}

templates/registration/password_change_done.html

<!-- templates/registration/password_change_done.html -->
{% extends 'base.html' %}

{% block title %}Password Change Successful{% endblock title %}

{% block content %}
    <h1>Password change successful</h1>
    <p>Your password was changed.</p>
{% endblock content %}

繼續(xù)刷新頁面(http://127.0.0.1:8000/accounts/password_change/),以看到我們的變化。

密碼重設(shè)

密碼重置處理了用戶忘記密碼的常見情況。其步驟與我們剛才所做的配置密碼更改非常相似。Django已經(jīng)提供了默認(rèn)的實現(xiàn),我們將使用它,然后定制模板,使其與我們網(wǎng)站的其他部分相匹配。

唯一需要的配置是告訴Django如何發(fā)送郵件。畢竟,用戶只有在能夠訪問與賬戶相連的電子郵件時才能重置密碼。在生產(chǎn)中我們將使用電子郵件服務(wù)SendGrid來實際發(fā)送電子郵件,但為了測試目的,我們可以依靠Django的控制臺后端設(shè)置,該設(shè)置將電子郵件文本輸出到我們的命令行控制臺。

在config/settings.py文件的底部做如下單行修改。

# config/settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

然后我們就都準(zhǔn)備好了! Django將為我們處理所有其他的事情。讓我們試試吧。導(dǎo)航到http://127.0.0.1:8000/accounts/password_reset/,查看默認(rèn)的密碼重置頁面。

確保你輸入的電子郵件地址與用戶賬戶相符。提交后,你將被重定向到密碼重置完成頁面。

其中說要檢查我們的電子郵件。由于我們已經(jīng)告訴Django將電子郵件發(fā)送到命令行控制臺,現(xiàn)在電子郵件文本將出現(xiàn)在那里。這是我在控制臺中看到的情況。

自定義模板

與 "更改密碼 "一樣,我們只需要創(chuàng)建新的模板來定制密碼重置的外觀

templates/registration/password_reset_form.html

<!-- templates/registration/password_reset_form.html -->
{% extends 'base.html' %}

{% block title %}Forgot Your Password?{% endblock title %}

{% block content %}
<h1>Forgot your password?</h1>
<p>Enter your email address below, and we'll email instructions
for setting a new one.</p>

<form method="POST">
  {% csrf_token %}
  {{ form.as_p }}
  <input class="btn btn-success" type="submit"
    value="Send me instructions!">
</form>
{% endblock content %}

templates/registration/password_reset_done.html

<!-- templates/registration/password_reset_done.html -->
{% extends 'base.html' %}

{% block title %}Email Sent{% endblock title %}

{% block content %}
  <h1>Check your inbox.</h1>
  <p>We've emailed you instructions for setting your password.
  You should receive the email shortly!</p>
{% endblock content %}

templates/registration/password_reset_confirm.html

<!-- templates/registration/password_reset_confirm.html -->
{% extends 'base.html' %}

{% block title %}Enter new password{% endblock title %}

{% block content %}
<h1>Set a new password!</h1>
<form method="POST">
  {% csrf_token %}
  {{ form.as_p }}
  <input class="btn btn-success" type="submit" value="Change my password">
</form>
{% endblock content %}

templates/registration/password_reset_complete.html

<!-- templates/registration/password_reset_complete.html -->
{% extends 'base.html' %}

{% block title %}Password reset complete{% endblock title %}

{% block content %}
<h1>Password reset complete</h1>
<p>Your new password has been set. You can log in now on the
<a href="{% url 'login' %}">Log In page</a>.</p>
{% endblock content %}

小結(jié)

在下一章中,我們將把我們的Newspaper應(yīng)用程序連接到電子郵件服務(wù)SendGrid上,這樣我們的自動電子郵件就可以真正地發(fā)送給用戶,而不是在命令行控制臺中輸出。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容