用户注册时我想要用户名
class SignUp(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
我需要访问已注册用户的用户名。
答案 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