如何从单个视图保存两个表单?

时间:2019-12-26 09:17:08

标签: django

我正在尝试创建一个包含其他信息的注册表单,例如“名字”,“电子邮件”等。我正在关注文章。我使用信号创建了一个名为“ Profile”的模型。用户注册后,只会更新用户,而不会更新“个人资料”模型。我试图创建一个模型表单以使用相同的注册视图更新“个人资料”模型,但无法正常工作。我在做什么错了?

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length = 50)
    email = models.EmailField(unique=True)
    phone = models.IntegerField(blank=True, null=True)
    bio = models.TextField(max_length=500, null =True, blank=True)
    location = models.CharField(max_length=30, null=True, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    # password1 = models.CharField(max_length=50)
    # password2 = models.CharField(max_length=50)



def update_user_profile(sender, instance, created, *args, **kwargs):
    if created:
        Profile.objects.create(user=instance)
        instance.profile.save()

post_save.connect(update_user_profile, sender = User)

forms.py

class SignUpForm(UserCreationForm):
    username = forms.CharField(max_length = 20, widget = forms.TextInput(attrs ={ 'class':'form-control', 'placeholder':'username'}
    ))
    first_name = forms.CharField(max_length = 20,  widget = forms.TextInput(attrs ={ 'class':'form-control', 'placeholder':'First Name'}
    ))
    last_name = forms.CharField(max_length = 20, widget = forms.TextInput(attrs ={ 'class':'form-control', 'placeholder':'Last Name'}
    ) )
    email = forms.CharField(max_length = 50,  widget = forms.TextInput(attrs ={ 'class':'form-control', 'placeholder': 'some@email.address'}
    ))
    birth_date = forms.DateField(widget = forms.TextInput(attrs ={ 'class':'form-control', 'placeholder': '1995-11-16'}
    ))
    phone = forms.IntegerField(widget = forms.TextInput(attrs ={ 'class':'form-control', 'placeholder': 'Phone Number'}
    ))
    password1 = forms.CharField(widget = forms.TextInput(attrs ={ 'class':'form-control', 'placeholder': 'password', 'type': 'password'}
    ))
    password2 = forms.CharField(widget = forms.TextInput(attrs ={ 'class':'form-control', 'placeholder': 'repeat password', 'type':'password'}
    ))
    class Meta:
        model = User
        fields = ('username','first_name', 'last_name', 'email', 'phone', 'birth_date', 'password1', 'password2' )

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['user', 'first_name', 'last_name', 'email', 'phone', 'birth_date']

views.py

class SignUp(View):
    def get(self, *args, **kwargs):
        form = SignUpForm
        profile = UserProfileForm
        context = { 'form': form, 'profile': profile}
        return render (self.request, 'registration/signup.html', context)

       @transaction.atomic
    def post(self, *args, **kwargs):
        form = SignUpForm(self.request.POST)
        # user_profile = UserProfileForm(self.request.POST)
        context = {'form': form, 'profile':user_profile}
        if form.is_valid():
            user = form.save()
            user.refresh_from_db()
            user.profile.first_name = form.cleaned_data.get('first_name')
            user.profile.last_name = form.cleaned_data.get('last_name')
            user.profile.email = form.cleaned_data.get('email')
            user.profile.birth_date = form.cleaned_data.get('birth_date')
            user.profile.phone = form.cleaned_data.get('phone')
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user_profile = UserProfileForm(self.request.POST, instance = user.profile)
            user_profile.full_clean()
            user_profile.save()
            user = authenticate(username=username, password=raw_password)
            return redirect('user-profile')
          return render (self.request, 'registration/signup.html', context)

谢谢!

0 个答案:

没有答案