gpt4 book ai didi

python - 在 django-registration 中使用电子邮件作为用户名

转载 作者:行者123 更新时间:2023-11-28 18:32:46 25 4
gpt4 key购买 nike

我在 Django 1.8 中使用 django-registration 2.0.4(https://django-registration.readthedocs.org/en/2.0.4/https://github.com/ubernostrum/django-registration),并尝试自定义注册表单和应用程序本身。虽然我可以自定义表单,但我仍然坚持使用电子邮件作为用户名。

我不需要更改 Django 的后端中的任何内容,因为只需删除表单中的用户名字段并在后端使用输入电子邮件作为用户名就足够了。

例如,我找到了这个 ( Django-Registration: Email as username),但它已经有几年历史了,我还没有设法让它运行起来。

我将如何以正确的方式实现上述功能?有没有人已经用最新版本解决了这个问题?

编辑:

因为我被要求提供代码:与这个问题没有太多相关,因为它不是代码问题,而是关于如何扩展或更改 django 插件的问题。

但是,我当前的无聊 forms.py,就像我链接的链接中一样:

class MyCustomForm(forms.Form):
email = Email(required=True)
password1 = forms.CharField(widget=forms.PasswordInput(), label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(), label="Repeat your password")

def clean_password(self):
if self.data['password1'] != self.data['password2']:
raise forms.ValidationError('Passwords are not the same')
return self.data['password1']

urls.py

...
url(r'^accounts/register/$', RegistrationView.as_view(form_class=InsuranceForm), name='registration_register'),
url(r'^accounts/', include('registration.backends.hmac.urls')),
...

最佳答案

我已经使用自定义用户模型完成了这项工作。

https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#auth-custom-user

上面的文档中有一个完整的示例,您必须稍作修改才能实现您的目标。

关于python - 在 django-registration 中使用电子邮件作为用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35326601/

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