Python扩展和自定义django-allauth

本文概述

  • Python3
  • Python3
  • Python3
  • Python3
先决条件:Django-allauth设置和配置
让我们处理自定义django-allauth注册表单, 干预注册流程以添加自定义流程和验证。
扩展注册表单或在django-allauth中添加自定义字段:
关于allauth最常见的查询之一是关于向注册表单添加额外字段或自定义字段。你可以从allauth.account.forms扩展SignupForm类。你所需要做的就是创建一个自定义类,将SignupForm传递给自定义类,定义自定义字段并保存它。返回用户对象非常重要,因为它将被传递给其他模块进行验证。你还需要在settings.py中包含一个变量。
让我们用一个forms.py示例来看看
Python3
from allauth.account.forms import SignupForm from django import forms class CustomSignupForm(SignupForm): first_name = forms.CharField(max_length = 30 , label = 'First Name' ) last_name = forms.CharField(max_length = 30 , label = 'Last Name' ) def signup( self , request, user): user.first_name = self .cleaned_data[ 'first_name' ] user.last_name = self .cleaned_data[ 'last_name' ] user.save() return user

在上面的代码片段中, CustomSignupForm进行了扩展, 该类继承了SignupForm类的所有功能并添加了必要的功能。在这里, 使用first_name和last_name命名的自定义字段是在同一类中使用注册模块创建并保存的。
【Python扩展和自定义django-allauth】上面代码的settings.py中的ACCOUNT_FORMS是
Python3
ACCOUNT_FORMS = { 'signup' : 'YourProject.forms.CustomSignupForm' , }

创建的任何其他自定义表单都可以在ACCOUNT_FORMS中进行扩展。该列表在文档中有说明(http://django-allauth.readthedocs.io/en/latest/forms.html#account-forms)。
类似地,LoginForm UserForm AddEmailForm和其他可以扩展。但是,请记住,当您扩展这些表单并在settings.py中链接它们时。不要忘记将原始表单(例如SignupForm)作为参数传递给你的类,有时您可能必须处理自定义验证并返回用户或其他值。请参考源代码以遵循正确的流程(https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py#L362)。
用户干预和自定义验证:
让我们讨论在用户注册流中添加自定义验证和干预。DefaultAccountAdapter非常有用,确实可以用来解决用户在使用django-allauth时可能遇到的大多数自定义问题。
示例1:电子邮件的受限列表
在找到存储和获取受限制列表的方法之后,当受限制的电子邮件试图注册时,您可以使用适配器并在注册表单中引发验证错误。扩展DefaultAccountAdapter并覆盖clean_email方法。在您的项目目录中创建一个adapter.py,并扩展默认的适配器类。
Python3
from allauth.account.adapter import DefaultAccountAdapter from django.forms import ValidationError class RestrictEmailAdapter(DefaultAccountAdapter): def clean_email( self , email): RestrictedList = [ 'Your restricted list goes here.' ] if email in RestrictedList raise ValidationError('You are restricted from registering.\ Please contact admin.') return email

最后, 将settings.py中的帐户适配器指向你的扩展类。
ACCOUNT_ADAPTER='YourProject.adapter.RestrictEmailAdapter'

示例2:为用户名添加最大长度
由于allauth库中不存在ACCOUNT_USERNAME_MAX_LENGTH, DefaultAccountAdaptercan用于轻松实现此功能。扩展DefaultAccountAdapterclass并覆盖clean_username方法。你还需要参考clean_username在我们的自定义验证之后, 再次完成其他内置验证。
上一段中的最后一句话是使用的关键DefaultAccountAdapter。你应该永远不要忘记为该模块引用原始模块名称, 以完成其他验证。
Python3
from allauth.account.adapter import DefaultAccountAdapter from django.forms import ValidationError class UsernameMaxAdapter(DefaultAccountAdapter): def clean_username( self , username): if len (username)> 'Your Max Size' : raise ValidationError('Please enter a username value\ less than the current one')# For other default validations. return DefaultAccountAdapter.clean_username( self , username)

最后, 指向settings.py中的子类
ACCOUNT_ADAPTER = 'YourProject.adapter.UsernameMaxAdapter'

你可以在源代码中引用adapter.py文件(https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py),并扩展其他模块,添加进程或更改它们的流。populate_username、clean_password等模块(可以通过定制来限制常用的密码)可以拥有定制的流程和流程,而无需重写它们。
如果在适当的情况下使用DefaultAccountAdapter来干预allauth的默认过程,那么DefaultAccountAdapter可能是一个强有力的工具。Allauth带有各种各样的内置设置,它们就在这里。你也可以从下面参考资料中的链接下载完整的源代码(http://django-allauth.readthedocs.io/en/latest/configuration.html)。
参考文献:
django-allauth文档
示例1上的堆栈溢出线程
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读