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

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

文章目录

一、场景需求

在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_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
# 重写重置密码表单
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相同属性的 username
            username = UserProfile.objects.get(tel=tel)
            user = User.objects.get(username=username)
            # 查看传参有无 令牌
            token_generator = kwargs.get(
                "token_generator", default_token_generator)
            # 没有生成token
            temp_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 admin
from django.urls import path, include
import userprofile.views
urlpatterns = [
    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'))
]
  • 效果:37.gif

参考文献:

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

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

raise 报异常异常用法

allauth 密码重置 *

as_view()解析

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



相关文章
|
存储 关系型数据库 MySQL
【Django | allauth】useprofile 用户模型扩展
【Django | allauth】useprofile 用户模型扩展
【Django | allauth】useprofile 用户模型扩展
|
关系型数据库 MySQL 数据库
【Django | allauth】登录_注册_邮箱验证_密码邮箱重置
【Django | allauth】登录_注册_邮箱验证_密码邮箱重置
【Django | allauth】登录_注册_邮箱验证_密码邮箱重置
|
关系型数据库 MySQL 数据库
【Django | allauth】登录_注册_邮箱验证_密码邮箱重置
【Django | allauth】登录_注册_邮箱验证_密码邮箱重置
【Django | allauth】登录_注册_邮箱验证_密码邮箱重置
|
JSON 数据可视化 前端开发
利用 Django 动态展示 Pyecharts 图表数据的几种方法
本文将介绍如何在 web 框架 Django 中使用可视化工具 Pyecharts, 看完本教程你将掌握几种动态展示可视化数据的方法!
利用 Django 动态展示 Pyecharts 图表数据的几种方法
|
SQL 缓存 Serverless
Django ORM性能优化之count和len方法的选择
Django ORM性能优化之count和len方法的选择
Django ORM性能优化之count和len方法的选择
|
Ubuntu 应用服务中间件 网络安全
Ubuntu部署Django项目方法详解
Ubuntu部署Django项目方法详解
1017 0
Ubuntu部署Django项目方法详解
|
Python
指定安装Django版本方法
(python2) E:\Evns>pip install django==1.9.8 安装方法命令:  pip install django==1.9.8
854 0
|
Python Windows 开发框架
Python3——创建Django项目的几种方法
Django是一种基于Python的Web开发框架 一、在命令行中创建Django项目(Windows平台、python3.
2035 0