当他/她在django注册时如何获取用户名?

时间:2018-11-26 10:07:29

标签: python django

用户注册时我想要用户名

class SignUp(generic.CreateView):
  form_class = UserCreationForm
  success_url = reverse_lazy('login')
  template_name = 'signup.html'

我需要访问已注册用户的用户名。

1 个答案:

答案 0 :(得分:-1)

您可以使用request.POST访问帖子数据。您可以覆盖post的请求 CreateView并添加特定代码以使用用户名执行所需操作,然后继续执行CreateView post方法/

的默认流程
def post(self, request, *args, **kwargs):
    username = request.POST.get('username')
    # do something with the username
    # and continue
    self.object = None
    return super().post(request, *args, **kwargs)

如果在将用户存储到数据库后需要使用该用户,则可以修改序列化程序的create方法,而不用修改post

class UserCreationForm:
    def create(self, validated_data):
        user = super().create(validated_data)
        username = user.username
        # do something with the username or the user
        return user