如何让用户在Django social-auth注册时设置用户名?

时间:2021-11-05 19:17:58

I use Django social-auth (omab version just to avoid any confusion with the other similarly named project) and right now I am trying it with Facebook. It is possible to register a new user and to login/logout without any issue. The only thing that I would like to add is a form during registration to let the user enter the desired username to be used on site because at the moment the username is either a facebook username (I do not want to force the user to use the same username) or a uuid if there is no facebook username (and that is ugly).

我使用Django social auth (omab版本,只是为了避免与另一个类似命名的项目混淆),现在我正在Facebook上试用。注册新用户和登录/注销都是可能的,没有任何问题。唯一我想添加在注册过程中是一种让用户输入所需的用户名在现场使用,因为目前的用户名是facebook的用户名(我不想强迫用户使用相同的用户名)或uuid如果没有facebook的用户名(这是丑陋)。

I am reading the docs, the pipelines and all that stuff but I'm not sure to understand, any hint or explanation would be welcome.

我正在阅读文档,管道和所有的东西,但我不确定我能理解,任何提示或解释都是欢迎的。

2 个解决方案

#1


2  

I found it in the example app that comes with social-auth https://github.com/omab/django-social-auth/tree/master/example/app. There is an example of the pipeline to use and even the form and views you need to implement. Very few to no changes are necessary to have a working implementation. At least some work needs to be done on the form at the time I write this because you can enter a username already taken.

我在带有social-auth https://github.com/omab/django-social-auth/tree/master/example/app的示例应用程序中找到了它。这里有一个要使用的管道示例,甚至还有需要实现的表单和视图。很少到不需要更改就可以实现工作。在我写这篇文章时,至少需要在表单上完成一些工作,因为您可以输入一个已经接受的用户名。

#2


0  

The accepted answer links to an entire GitHub project without explaining anything about what parts of it are relevant, and some of it is outdated, so I'll try to share what I've learned.

被接受的答案链接到一个完整的GitHub项目,而没有解释它的哪些部分是相关的,有些是过时的,所以我将尝试分享我学到的东西。

django-social-auth is deprecated and the replacement is social-app-django, which integrates Django with the python-social-auth project.

Django -social-auth被弃用,取而代之的是social-app-django,它将Django与python-social-auth项目整合在一起。

The documentation on python-social-auth Pipelines is relevant. In the default pipeline, this is the stage that generates the username:

python-social-auth管道的文档是相关的。在默认的管道中,这是生成用户名的阶段:

# Make up a username for this person, appends a random string at the end if
# there's any collision.
'social_core.pipeline.user.get_username',

The implementation of get_username shows the default behavior. We will have to copy these aspects:

get_username的实现显示默认行为。我们必须复制这些方面:

  • It ensures that the username it comes up with is unique, by checking storage.user.user_exists(username=...) and modifying the username until it is unique.
  • 它通过检查storage.user.user_exists(username=…)并修改用户名,直到用户名是惟一的,从而确保所提供的用户名是惟一的。
  • It returns the dictionary {'username': '...'}, which is passed to the next stages in the pipeline.
  • 它返回字典{'username': '…'},传递到管道的下一个阶段。

To prompt the user, we need a custom "partial" pipeline stage. This lets us pause the pipeline to wait for the user to submit the username form and then resume it once we have the username.

要提示用户,我们需要一个自定义的“部分”管道阶段。这让我们暂停管道,等待用户提交用户名表单,然后在获得用户名后再继续。

#1


2  

I found it in the example app that comes with social-auth https://github.com/omab/django-social-auth/tree/master/example/app. There is an example of the pipeline to use and even the form and views you need to implement. Very few to no changes are necessary to have a working implementation. At least some work needs to be done on the form at the time I write this because you can enter a username already taken.

我在带有social-auth https://github.com/omab/django-social-auth/tree/master/example/app的示例应用程序中找到了它。这里有一个要使用的管道示例,甚至还有需要实现的表单和视图。很少到不需要更改就可以实现工作。在我写这篇文章时,至少需要在表单上完成一些工作,因为您可以输入一个已经接受的用户名。

#2


0  

The accepted answer links to an entire GitHub project without explaining anything about what parts of it are relevant, and some of it is outdated, so I'll try to share what I've learned.

被接受的答案链接到一个完整的GitHub项目,而没有解释它的哪些部分是相关的,有些是过时的,所以我将尝试分享我学到的东西。

django-social-auth is deprecated and the replacement is social-app-django, which integrates Django with the python-social-auth project.

Django -social-auth被弃用,取而代之的是social-app-django,它将Django与python-social-auth项目整合在一起。

The documentation on python-social-auth Pipelines is relevant. In the default pipeline, this is the stage that generates the username:

python-social-auth管道的文档是相关的。在默认的管道中,这是生成用户名的阶段:

# Make up a username for this person, appends a random string at the end if
# there's any collision.
'social_core.pipeline.user.get_username',

The implementation of get_username shows the default behavior. We will have to copy these aspects:

get_username的实现显示默认行为。我们必须复制这些方面:

  • It ensures that the username it comes up with is unique, by checking storage.user.user_exists(username=...) and modifying the username until it is unique.
  • 它通过检查storage.user.user_exists(username=…)并修改用户名,直到用户名是惟一的,从而确保所提供的用户名是惟一的。
  • It returns the dictionary {'username': '...'}, which is passed to the next stages in the pipeline.
  • 它返回字典{'username': '…'},传递到管道的下一个阶段。

To prompt the user, we need a custom "partial" pipeline stage. This lets us pause the pipeline to wait for the user to submit the username form and then resume it once we have the username.

要提示用户,我们需要一个自定义的“部分”管道阶段。这让我们暂停管道,等待用户提交用户名表单,然后在获得用户名后再继续。