Django通用類視圖實(shí)現(xiàn)忘記密碼重置密碼功能示例
前言
在Django中有大量的通用類視圖,例如ListView,DetailView,CreateView,UpdateView等等,將所有重復(fù)的增刪改查代碼抽象成一個(gè)通用類,只需要配置極少量的代碼即可實(shí)現(xiàn)功能。
使用通用類視圖完成找回密碼功能
首先引入
from django.contrib.auth.views import PasswordResetView, PasswordResetConfirmView, \ PasswordResetDoneView, PasswordChangeView, PasswordChangeDoneView, \ PasswordResetCompleteView
配置如下:
class MyPasswordResetView(PasswordResetView): """重置密碼視圖""" template_name = 'users/registration/forget_pwd.html' form_class = ForgetForm success_url = reverse_lazy("users:password_reset_done") email_template_name = 'users/registration/password_reset_email.html' class MyPasswordResetConfirmView(PasswordResetConfirmView): """重置密碼頁(yè)面,輸入兩次密碼""" template_name = 'users/registration/password_change_form.html' success_url = reverse_lazy('users:password_reset_complete') class MyPasswordResetDoneView(PasswordResetDoneView): """發(fā)送確認(rèn)重置郵件""" template_name = 'users/registration/password_reset_done.html' class MyPasswordResetCompleteView(PasswordResetCompleteView): """完成重置密碼""" template_name = 'users/registration/password_change_done.html'
其中忘記密碼,填寫郵箱的模板forget_pwd.html模板如下:
<form method="post" class="form-validate" action="{% url 'users:password_reset' %}"> <div class="form-group"> <input id="login-username" type="text" name="email" required data-msg="請(qǐng)輸入您的郵箱" class="input-material"> <label for="login-username" class="label-material">郵箱</label> {% if form.errors %} <div style="color: red">郵箱輸入錯(cuò)誤</div> {% endif %} </div> {% csrf_token %} <button type="submit" id="login" href="#" rel="external nofollow" class="btn btn-primary">發(fā)送確認(rèn)郵件</button> <!-- This should be submit button but I replaced it with <a> for demo purposes--> </form>
其中輸入新密碼模板password_change_form.html頁(yè)面如下:
<form id="form" method="post"> {% csrf_token %} {{ form|crispy }} <div class="form-group"> <button type="submit" class="btn btn-primary button-submit">確認(rèn)更改</button> </div> </form>
其中重置密碼郵件發(fā)送成功的模板password_reset_done.html如下:
<div class="col-lg-6 bg-white"> <div class="form d-flex align-items-center"> <div class="content"> <h1>重置密碼郵件發(fā)送成功!</h1> </div> </div> </div>
其中密碼重置成功password_change_done.html如下:
<div class="container"> <div class="row"> <div class="col-md-6 offset-md-3"> <h1>重置密碼成功!</h1> <a href="{% url 'index' %}" rel="external nofollow" >回到首頁(yè)</a> </div> </div> </div>
最后配置路由
path('password/reset/', view=MyPasswordResetView.as_view(), name='password_reset'), # password_reset path('password/reset/done/', MyPasswordResetDoneView.as_view(), name='password_reset_done'), # password_reset_done url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', view=MyPasswordResetConfirmView.as_view(), name='password_reset_confirm'), # reset path('reset/done/', MyPasswordResetCompleteView.as_view(), name='password_reset_complete'),
效果如下:
以上這篇Django通用類視圖實(shí)現(xiàn)忘記密碼重置密碼功能示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 抓取微信公眾號(hào)賬號(hào)信息的方法
搜狗微信搜索提供兩種類型的關(guān)鍵詞搜索,一種是搜索公眾號(hào)文章內(nèi)容,另一種是直接搜索微信公眾號(hào)。這篇文章主要介紹了Python 抓取微信公眾號(hào)賬號(hào)信息,需要的朋友可以參考下2019-06-06Appium+Python+pytest自動(dòng)化測(cè)試框架的實(shí)戰(zhàn)
本文主要介紹了Appium+Python+pytest自動(dòng)化測(cè)試框架的實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12基于Python __dict__與dir()的區(qū)別詳解
下面小編就為大家?guī)?lái)一篇基于Python __dict__與dir()的區(qū)別詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Python如何利用pandas讀取csv數(shù)據(jù)并繪圖
這篇文章主要介紹了Python如何利用pandas讀取csv數(shù)據(jù)并繪圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Python多進(jìn)程與服務(wù)器并發(fā)原理及用法實(shí)例分析
這篇文章主要介紹了Python多進(jìn)程與服務(wù)器并發(fā)原理及用法,深入淺出的介紹了進(jìn)程、并行、并發(fā)、同步、異步等相關(guān)概念與原理,并結(jié)合實(shí)例形式給出了Python多進(jìn)程編程相關(guān)操作技巧,需要的朋友可以參考下2018-08-08PyTorch零基礎(chǔ)入門之構(gòu)建模型基礎(chǔ)
PyTorch是一個(gè)開源的Python機(jī)器學(xué)習(xí)庫(kù),基于Torch,用于自然語(yǔ)言處理等應(yīng)用程序,它是一個(gè)可續(xù)計(jì)算包,提供兩個(gè)高級(jí)功能:1、具有強(qiáng)大的GPU加速的張量計(jì)算(如NumPy)。2、包含自動(dòng)求導(dǎo)系統(tǒng)的深度神經(jīng)網(wǎng)絡(luò)2021-10-10python實(shí)現(xiàn)折半查找和歸并排序算法
這篇文章主要介紹了python實(shí)現(xiàn)折半查找和歸并排序算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04