django-allauth设置用户名与电子邮件相同 [英] django-allauth set username the same as email

查看:225
本文介绍了django-allauth设置用户名与电子邮件相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册表单,只要求邮件和密码。当用户注册时,django-allauth通过从用户的电子邮件地址中分离@email后缀,为该用户创建一个用户名。



所以例如,如果用户注册 some-user@example.com ,他的用户名将是 某些用户 ,如果另一个用户注册 some-user@gmail.com ,那么他的用户名将是 some-userr



但是我想要的是用户的用户名和电子邮件具有相同的值。



那么如何配置django-allauth来将用户名设置为用户的电子邮件,而不会使他们后缀?



如果可能,如何创建自定义用户。



在我的设置中.py:

  ###################### ## 
#AllAuth配置#
######################
ACCOUNT_AUTHENTICATION_METHOD ='email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = Tr ue
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION ='mandatory'
ACCOUNT_PASSWORD_MIN_LENGTH = 8


解决方案

我使用的是用户pre_save上的信号。



您的设置看起来很好,所以如果您可以在以下代码中添加以下代码:例如 core.models.py 它将按照您的需要工作:

  @receiver(pre_save,sender = User)
def update_username_from_email(发件人,实例,** kwargs):
user_email = instance.email
username = user_email [:30]
n = 1
而User.objects.exclude(pk = instance.pk).filter(username = username).exists():
n + = 1
username = user_email [:( 29 - len(str(n)))] +' - '+ str(n)
instance.username =用户名

我用信号做的这个原因是,每当用户被保存时,用户名被更新。您可以检查电子邮件是否已更改,仅在这种情况下更新用户名。



然后我将用户名限制为电子邮件的前30个字符(默认最大长度用户名为30个字符):

  username = user_email [:30] 
pre>

您也可以更改用户名的最大长度,但在我的情况下,我更喜欢使用默认的长度。



由于我这样做,可能会发生重复的用户名。为了避免重复的用户名,如果生成的用户名限制为30个字符的情况下,我将-2,-3 ...放在最后,使用户名唯一:

  n = 1 
而User.objects.exclude(pk = instance.pk).filter(username = username).exists():
n + = 1
username = user_email [:( 29 - len(str(n)))] +' - '+ str(n)
instance.username =用户名

我希望这个解决方案可以帮助你!


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.

So for example, if a user signs up with "some-user@example.com" his username will be "some-user" and if another user signs up with "some-user@gmail.com" then his username will be "some-userr"

But what I want is the username and email of the users to have the same value.

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.

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

解决方案

I do exactly what you want to do with a signal on User pre_save.

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.

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.

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设置用户名与电子邮件相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆