【发布时间】:2012-03-22 18:18:32
【问题描述】:
我目前正在为应用实施用户注册。我想将 django.auth 默认行为更改为使用电子邮件而不是用户名。我想我会使用自定义身份验证后端。我希望用户仍然能够提供用户名。
如何在不更改 django.auth 模型中的字段的情况下允许用户名中的空格(如“john doe”)。
如何使电子邮件字段成为必填项。目前,如果电子邮件为空,我的 UserCreation 表单中的 clean_email 方法会引发验证错误。一定有更好的办法。
感谢 Brandon 和 Andrew Sledge,这是我的解决方案:
class RegisterForm(UserCreationForm):
username = forms.RegexField(label=("Username"), max_length=30, regex=r'^[ \t\r\n\f\w.@+*-]+$',
help_text = ("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
error_messages = {'invalid': ("This value may contain only letters, numbers and @/./+/-/_ characters.")})
class Meta:
model = User
我从 UserCreationForm 复制了该字段。重要的部分是将 \t\r\n\f 添加到正则表达式。 \s 不会工作。
【问题讨论】:
标签: django django-authentication