问题描述
我有一个只要求输入电子邮件和密码的注册表单.当用户注册时,django-allauth 通过从用户的电子邮件地址中去除@email"后缀来为该用户创建一个用户名.
I have a sign up form which asks only for email and password. When a user signs up, django-allauth creates a username for that user by striping the "@email" suffix form the user's email address.
例如,如果用户使用[email protected]"注册,他的用户名将是some-user>",如果另一个用户使用[email protected]"注册,那么他的用户名将是some-userr"
So for example, if a user signs up with "[email protected]" his username will be "some-user" and if another user signs up with "[email protected]" then his username will be "some-userr"
但我想要的是用户的用户名和电子邮件具有相同的值.
But what I want is the username and email of the users to have the same value.
那么我如何配置 django-allauth 以将用户名设置为用户电子邮件而不剥离他们的后缀?
So how can I configure django-allauth to set the usernames as the users emails without striping their suffixes?
如果可能的话,如何在不创建自定义用户的情况下做到这一点.
And if possible, how can I do that without creating a custom user.
在我的 settings.py 中:
In my settings.py:
#########################
# AllAuth Configuration #
#########################
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_PASSWORD_MIN_LENGTH = 8
推荐答案
我对 User pre_save 上的信号做你想做的事.
I do exactly what you want to do with a signal on User pre_save.
您的设置看起来没问题,所以如果您在诸如 core.models.py
之类的地方添加以下代码,它将按您的需要工作:
Your settings look ok, so if you add the following code in somewhere like for example core.models.py
it will work as you need:
@receiver(pre_save, sender=User)
def update_username_from_email(sender, instance, **kwargs):
user_email = instance.email
username = user_email[:30]
n = 1
while User.objects.exclude(pk=instance.pk).filter(username=username).exists():
n += 1
username = user_email[:(29 - len(str(n)))] + '-' + str(n)
instance.username = username
我用信号来做的原因是我希望每次保存用户时,用户名都会更新.您可以检查电子邮件是否已更改,只有在这种情况下才更新用户名.
The reason I do it with a signal is that I want that every time User is saved, the username is updated. You could check if the e-mail has changed update the username only in that case.
然后我将用户名限制为电子邮件的前 30 个字符(用户名的默认最大长度为 30 个字符):
Then I limit the username to the first 30 characters of email (default max lenght of username is 30 chars):
username = user_email[:30]
您也可以更改用户名的最大长度,但就我而言,我更喜欢使用默认长度.
You could also change the max lenght of username, but in my case I prefered to use the default lenght.
自从我这样做以来,可能会发生重复的用户名.为避免重复用户名,如果限制为 30 个字符后生成的用户名已经存在,我将 -2、-3... 放在末尾以使用户名唯一:
Since I make this, it could happen that there are repeated usernames. To avoid repeated usernames, in case that the resulting username after limiting it to 30 characters already exists, I put -2, -3... at the end to make the username unique:
n = 1
while User.objects.exclude(pk=instance.pk).filter(username=username).exists():
n += 1
username = user_email[:(29 - len(str(n)))] + '-' + str(n)
instance.username = username
希望这个解决方案能帮到你!
I hope this solution helps you!
这篇关于django-allauth 将用户名设置为与电子邮件相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!