100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 【Django | allauth】重写allauth重置密码方法

【Django | allauth】重写allauth重置密码方法

时间:2023-11-05 21:58:43

相关推荐

【Django | allauth】重写allauth重置密码方法

🤵‍♂️ 个人主页: @计算机魔术师

👨‍💻 作者简介:CSDN内容合伙人,全栈领域优质创作者。

🌐 推荐一款找工作神器网站: 牛客网🎉🎉|笔试题库|面试经验|实习招聘内推

还没账户的小伙伴 速速点击链接跳转牛客网登录注册 开始刷爆题库,速速通关面试吧🙋‍♂️

该文章收录专栏

✨—【Django | 项目开发】从入门到上线 专栏—✨

一、场景需求

在allauth 中默认重置密码的方式是用户发送重置密码的请求后,发送重置密码的链接到用户的邮箱里面重置密码,如果使用QQ邮箱的SMTP服务,一天最多只能发送50封邮件,这样是明显不满足需求的,而如果为了实现此功能去部署一台邮件服务器或者申请一个企业邮箱,动辄几千一年的费用实在伤不起。所以在中小型的项目中,有一种折中的方法,即用户通过输入自己的身份证[这里已电话为例]即可重置对应的账号密码。

二、重写表单模型

form.py添加表单模型 (处理手机号)

from django import forms# 重写重置密码表单class ResetPasswordForm(forms.Form):"""重置密码表单,需要手机号验证"""tel = forms.CharField(max_length=20, required=True, label='Telephone')# 获取电话号码def clean_identity_tel(self):tel = self.cleaned_data['tel']print(tel)"""由于用get获取对象,如果获取不到会报错,所以这里使用filter获取失败返回空对象列表在UserProfile中筛选符合条件的用户,返回用户名"""username = UserProfile.objects.filter(tel=tel)if not username:raise forms.ValidationError("手机号错误!!")return self.cleaned_data['tel']def save(self, request, **kwargs):return self.cleaned_data['tel']

三、重写view视图函数类

allauth中的重置密码的类视图位于allauth.account.views.PasswordResetView,我们需要在views.py中继承这个类并且重写它的post方法。

view.py视图函数

注意!!这里的default_token_generator函数是allauth中的form.py的函数,不是django.contib,auth.token的,不然会报bad token错误,因为生成token的方法是不一样的(还有邮箱等)

from allauth.account.forms import default_token_generator,SignupForm # 注意!! token生成实在allauth里面,不是django自带得token生成器from allauth.account.utils import user_pk_to_url_strfrom allauth.account.views import PasswordResetViewfrom django.contrib.auth.decorators import login_requiredfrom django.contrib.auth.models import Userfrom django.http import HttpRequestfrom django.shortcuts import render, get_object_or_404, reverse, redirect, HttpResponseRedirectfrom userprofile.forms import UseProfileForm, ResetPasswordFormfrom userprofile.models import UserProfile# 重写重置密码表单class CustomPasswordResetView(PasswordResetView):def post(self, request, *args, **kwargs):reset_password_form = ResetPasswordForm(request.POST)if reset_password_form.is_valid():# 从电话筛选出 用户对象tel = reset_password_form.clean_identity_tel()# UseProfile 中由于user相同属性的 usernameusername = UserProfile.objects.get(tel=tel)user = User.objects.get(username=username)# 查看传参有无 令牌token_generator = kwargs.get("token_generator", default_token_generator)# 没有生成tokentemp_key = token_generator.make_token(user)# 反向解析路径,(并传令牌参数)path = reverse("account_reset_password_from_key",kwargs=dict(uidb36=user_pk_to_url_str(user), key=temp_key),)# 在根目录下建立绝对路径(self = request)url = HttpRequest.build_absolute_uri(request, path)# 重定向至修改密码链接return redirect(url)else:return render(request, 'account/telephone_error.html', {'content': "电话错误(表单格式错误)"})# 注意 这里不能加上 login_required 的限制! 不然登录页面 忘记密码就会成功跳转页面!password_reset = CustomPasswordResetView.as_view()

setting.py添加配置(重写表单选项)

ACCOUNT_FORMS = ({'reset_password': 'Userprofile.forms.ResetPasswordForm'})

五、配置项目路由

注意!!!: 在 引入 扩展模型应用路由时allauth应用 和 userprofile 谁在上方一定要考虑好,不然路由覆盖等会出现页面失效或者报错的情况!!(一般默认allauth在上方),这里为了实现密码重置,要让account/password/reset不能走 allauth 的注册视图类,又不能修改allauth 源码,此时我们使用继承并在 项目 路由修改优先级,优先进去扩展应用模型的 重写密码类。

项目urls.py

from django.contrib import adminfrom django.urls import path, includeimport userprofile.viewsurlpatterns = [path('admin/', admin.site.urls),path('', userprofile.views.profile), # 首页 则为信息页(当未登录 自动跳转到login页)# 注意路由最后 一个 /path('accounts/password/reset/', userprofile.views.password_reset, name='account_reset_password'),path('accounts/', include('allauth.urls')),path('accounts/', include('userprofile.urls'))]

效果:

参考文献:

Django的objects.get和objects.filter方法详解和区别

Python中的*(星号)和**(双星号)完全详解

raise 报异常异常用法

allauth 密码重置 *

as_view()解析

✨谢谢你的阅读,你的点赞和收藏是我创作的最大动力✨

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。