1 回答
TA贡献1875条经验 获得超3个赞
我认为你写的两个信号是不必要的。此外,此行也不起作用:
profile.user = Profile.objects.get(user=request.user)
因为您尚未登录。未登录,您将无法在此处获取用户实例。但没有必要。因为,您可以从另一个窗体获取用户实例。喜欢这个:
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
profile_form = ProfileForm(request.POST, request.FILES)
if form.is_valid() and profile_form.is_valid():
user = form.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Your account has been created! You are now able to log in')
return redirect('login')
else:
form = UserRegisterForm()
profile_form = ProfileForm()
return render(request, 'users/register.html', {'form': form, 'profile_form': profile_form})
另外,请务必删除这两个信号。
添加回答
举报