gpt4 book ai didi

python - 如何将 max_length 添加到 allauth 用户名

转载 作者:行者123 更新时间:2023-12-01 09:24:26 26 4
gpt4 key购买 nike

我使用 Django allauth 作为我的 Django 站点的用户帐户框架。文档显示有一个 ACCOUNT_USERNAME_MIN_LENGTH但是由于某种原因没有 ACCOUNT_USERNAME_MAX_LENGTH

有什么方法可以设置用户名的最大长度吗?

这是我的自定义 allauth 注册表单 - 也许我可以在这里做点什么?:

class AllauthSignupForm(forms.Form):

captcha = ReCaptchaField(
public_key=config("RECAPTCHA_PUBLIC_KEY"),
private_key=config("RECAPTCHA_PRIVATE_KEY"),
)

class Meta:
model = User

def signup(self, request, user):
""" Required, or else it throws deprecation warnings """
pass

编辑:尝试子类化 SignupView

draft1/forms.py

class AllauthSignupForm(SignupForm):
def __init__(self, *args, **kwargs):
super(AllauthSignupForm, self).__init__(*args, **kwargs)
self.fields['username']['validators'] += MaxLengthValidator(150,
"Username should be less than 150 character long")

captcha = ReCaptchaField(
public_key=config("RECAPTCHA_PUBLIC_KEY"),
private_key=config("RECAPTCHA_PRIVATE_KEY"),
)

class Meta:
model = User

def signup(self, request, user):
""" Required, or else it throws deprecation warnings """
pass

draft1/views.py

from allauth.account.views import SignupView

class MySignupView(SignupView):
form_class = AllauthSignupForm

allauth/account/urls.py

url(r"^signup/$", MySignupView.as_view(), name="account_signup"),

draft1/settings.py

ACCOUNT_SIGNUP_FORM_CLASS = 'draft1.forms.AllauthSignupForm'

上面的代码返回此错误:

Traceback (most recent call last):
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/Users/zorgan/Desktop/postr1/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/zorgan/Desktop/vorsso/venvor/draft1/urls.py", line 6, in <module>
from . import views
File "/Users/zorgan/Desktop/vorsso/venvor/draft1/views.py", line 11, in <module>
from .forms import UserSettingsForm
File "/Users/zorgan/Desktop/vorsso/venvor/draft1/forms.py", line 8, in <module>
from allauth.account.forms import SignupForm
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/allauth/account/forms.py", line 228, in <module>
class BaseSignupForm(_base_signup_form_class()):
File "/Users/zorgan/Desktop/postr1/lib/python3.5/site-packages/allauth/account/forms.py", line 216, in _base_signup_form_class
fc_classname))
django.core.exceptions.ImproperlyConfigured: Module "draft1.forms" does not define a "AllauthSignupForm" class

最佳答案

这可以通过扩展 DefaultAccountAdapter 类并重写 clean_username 方法来快速完成。在我们的自定义验证之后,您还需要再次引用 clean_username 以完成其他内置验证。

可以按如下方式完成。

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')
return DefaultAccountAdapter.clean_username(self,username) # For other default validations.

最后,指向 settings.py 中的子类

ACCOUNT_ADAPTER = 'YourProject.adapter.UsernameMaxAdapter'

引用:https://github.com/pennersr/django-allauth/blob/8fbbf8c1d32832d72de5ed1c7fd77600af57ea6f/allauth/account/adapter.py#L244

关于python - 如何将 max_length 添加到 allauth 用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50548685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com