当前位置:网站首页>Live video source code, reset the relevant changes of the current password
Live video source code, reset the relevant changes of the current password
2022-07-23 06:20:00 【Cloudleopard network technology】
Live video source , Relevant changes to reset the current password
One 、 Scenario requirements
stay allauth The default way to reset the password in is after the user sends the request to reset the password , Send a link to reset the password to the user's email to reset the password , If you use QQ Mailbox SMTP service , You can only send... At most in a day 50 Messages , This obviously does not meet the demand , If you deploy a mail server or apply for a corporate mailbox to achieve this function , I can't afford to spend thousands of years . So in small and medium-sized projects , There is a compromise , That is, users enter their own ID card [ Here is the telephone as an example ] You can reset the corresponding account password .
Two 、 Rewrite the form model
stay form.py Add form model ( Deal with mobile phone numbers )
from django import forms
# Rewrite the reset password form
class ResetPasswordForm(forms.Form):
"""
Reset password form , Mobile phone number verification is required
"""
tel = forms.CharField(max_length=20, required=True, label='Telephone')
# Get the phone number
def clean_identity_tel(self):
tel = self.cleaned_data['tel']
print(tel)
"""
Because of the use of get Get objects , If not, an error will be reported , So here we use filter
Get failure returns an empty object list
stay UserProfile Filter qualified users in , Return user name
"""
username = UserProfile.objects.filter(tel=tel)
if not username:
raise forms.ValidationError(" Wrong cell phone number !!")
return self.cleaned_data['tel']
def save(self, request, **kwargs):
return self.cleaned_data['tel']
3、 ... and 、 rewrite view View function class
allauth The class view for resetting passwords in is located in allauth.account.views.PasswordResetView, We need to be in views.py Inherits this class and overrides its post Method .
stay view.py The view function
Be careful !!: there default_token_generator The function is allauth Medium form.py Function of , No django.contib,auth.token Of , Otherwise it will be reported bad token error , Because it generates token The method is different ( And email, etc )
from allauth.account.forms import default_token_generator,SignupForm # Be careful !! token Generating reality allauth Inside , No django Bring your own token generator
from allauth.account.utils import user_pk_to_url_str
from allauth.account.views import PasswordResetView
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpRequest
from django.shortcuts import render, get_object_or_404, reverse, redirect, HttpResponseRedirect
from userprofile.forms import UseProfileForm, ResetPasswordForm
from userprofile.models import UserProfile
# Rewrite the reset password form
class CustomPasswordResetView(PasswordResetView):
def post(self, request, *args, **kwargs):
reset_password_form = ResetPasswordForm(request.POST)
if reset_password_form.is_valid():
# Screen out from the phone User object
tel = reset_password_form.clean_identity_tel()
# UseProfile Due to user Of the same property username
username = UserProfile.objects.get(tel=tel)
user = User.objects.get(username=username)
# Check whether the parameter is transferred token
token_generator = kwargs.get(
"token_generator", default_token_generator)
# No generation token
temp_key = token_generator.make_token(user)
# Reverse resolution path ,( And herald board parameters )
path = reverse(
"account_reset_password_from_key",
kwargs=dict(uidb36=user_pk_to_url_str(user), key=temp_key),
)
# Establish an absolute path under the root directory (self = request)
url = HttpRequest.build_absolute_uri(request, path)
# Redirect to the change password link
return redirect(url)
else:
return render(request, 'account/telephone_error.html', {
'content': " Telephone error ( Form format error )"})
# Be careful You can't add... Here login_required The limitation of ! Otherwise, the login page If you forget your password, you will successfully jump to the page !
password_reset = CustomPasswordResetView.as_view()
stay setting.py Add the configuration ( Rewrite form options )
ACCOUNT_FORMS = ({
'reset_password': 'Userprofile.forms.ResetPasswordForm'
})
Four 、 Configure project routing
Be careful !!!: stay introduce When the extended model applies routing allauth application and userprofile Who is at the top must be considered , Otherwise, there will be page failure or error reporting in routing coverage !!( General default allauth On the top ), Here, in order to reset the password , Must let account/password/reset Can't go allauth Registered view class for , It can't be modified allauth Source code , At this point, we use inheritance and in project Route modification priority , Give priority to expanding the application model Rewrite password class .
project urls.py
from django.contrib import admin
from django.urls import path, include
import userprofile.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', userprofile.views.profile), # home page Is the information page ( When not logged in Auto jump to login page )
# Pay attention to the end of the route One /
path('accounts/password/reset/', userprofile.views.password_reset, name='account_reset_password'),
path('accounts/', include('allauth.urls')),
path('accounts/', include('userprofile.urls'))
]
That's all Live video source , Relevant changes to reset the current password , More content welcome to follow the article
边栏推荐
- 日常记账后,项目图表显示各种收支类别
- Conditions affecting interface query speed
- Over fitting weight regularization and dropout regularization
- [第五空间2019 决赛]PWN5 ——两种解法
- Pile up basic exercises - 1
- 3步就能制作漫画头像的机器人,想拥有一个吗?
- 接口文档进化图鉴, 用过第一款接口文档工具的人暴露年龄了
- 关于博主帅soserious的一些感想.
- Video color removal, teach you simple and uncomplicated operation methods
- 全球首个航天大模型问世,文心秒补《富春山居图》,这是百度普惠AI的恒心...
猜你喜欢
随机推荐
pwn1_sctf_2016
Video color removal, teach you simple and uncomplicated operation methods
Using "hifolw" to quickly create the information generation of College Students' return list
最大连续子序列--每日一题
[SUCTF 2019]EasySQL
关于博主帅soserious的一些感想.
2020_ACL_A Transformer-based joint-encoding for Emotion Recognition and Sentiment Analysis
2019_ ACL_ Multimodal Transformer for Unaligned Multimodal Language Sequences
Conditions affecting interface query speed
pwn1_ sctf_ two thousand and sixteen
PWN —— ret2libc2
Lc: sword finger offer 05. replace spaces
The simplest scull device driver
NLP language model
win11任务管理器怎么打开?win11任务管理器打开的技巧方法
中国科学院院士王怀民:推进中国开源创新联合体的思考与实践
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-3 personal solutions
NLP-语言模型
Pytoch realizes text emotion analysis
IM即时通讯开发时手机信号为什么会差









