Python 在一个页面中保存多个表单中的数据

Python 在一个页面中保存多个表单中的数据,python,django,Python,Django,我有一个页面有两种形式。一个表单在用户单击编辑按钮后显示为弹出窗口 我无法保存单击“编辑”按钮后弹出的表单中的信息。不知道我做错了什么 这就是我的看法 def payment_view(request): form = MentorPaymentForm() if request.method == 'POST': form = MentorPaymentForm(request.POST, request.FILES, instance=request.user

我有一个页面有两种形式。一个表单在用户单击编辑按钮后显示为弹出窗口

我无法保存单击“编辑”按钮后弹出的表单中的信息。不知道我做错了什么

这就是我的看法

def payment_view(request):
    form = MentorPaymentForm()
    if request.method == 'POST':
        form = MentorPaymentForm(request.POST, request.FILES, instance=request.user)
        if form.is_valid():
            user,mentor = form.save(commit=False)
            return redirect('teachers:payment_view')
    else:
        form = MentorPaymentForm(instance=request.user)
    return render(request, 'classroom/teachers/app-instructor-billing.html', {'form': form})
我的模板:

<form id="billing_details", method="post">
                        {% csrf_token %}
                          <label class="col-md-2 control-label">Account Number</label>
                          <div class="form-group form-control-material">
                            {{ form.account_num }}
                          </div>
                          <label for="bankname" class="col-md-2 control-label">Bank Name</label>
                          <div class="form-group form-control-material" id="bankname">
                            {{ form.bank_name }}
                          </div>
                          <label for="acc_num" class="col-md-2 control-label">Branch Code</label>
                          <div class="form-group form-control-material">
                            {{ form.branch_code }}
                          </div>
                          <button type="submit" class="btn btn-success paper-shadow relative" data-z="0.5" data-hover-z="1" data-animated data-dismiss="modal">Update Payment Details</button>
                        </form>
更新

正如在这里的评论中所要求的,当我删除commit=False时,将进行完整的回溯

Traceback:

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "C:\Users\User\Desktop\djangoproject\django-multiple-user-types-example-master\django_school\classroom\views\teachers.py" in payment_view
  124.             user,mentor = form.save(commit=False)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\functional.py" in inner
  257.         return func(self._wrapped, *args)

Exception Type: TypeError at /teachers/payment_view
Exception Value: 'User' object is not iterable
及表格

class TeacherSignUpForm(UserCreationForm):
    email = forms.EmailField(max_length=100)
    first_name = forms.CharField(max_length=100)
    last_name = forms.CharField(max_length=100)
    linkedin = forms.URLField(max_length=200)
    address = forms.CharField(max_length=500)
    billing_name = forms.CharField(max_length=200)
    account_num = forms.IntegerField()
    bank_name = forms.CharField(max_length=50)
    branch_code = forms.IntegerField()

    class Meta(UserCreationForm.Meta):
        model = User
        fields = ('email', 'username', 'first_name', 'last_name')

    def save(self, commit=True):
        self.instance.is_teacher = True
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()
        mentor = Mentor.objects.get_or_create(
            user=user,
            linkedin=self.cleaned_data['linkedin'],
            address=self.cleaned_data['address'],
            billing_name=self.cleaned_data['billing_name'],
            account_num=self.cleaned_data['account_num'],
            bank_name=self.cleaned_data['bank_name'],
            branch_code=self.cleaned_data['branch_code'],
        )
        return user
...
class MentorPaymentForm(forms.ModelForm):
    class Meta:
        model = Mentor
        fields = ('address', 'billing_name', 'account_num', 'bank_name', 'branch_code')

user,mentor=form.save(commit=False)
可以是
form.save()

我有一个包含4个表单的页面,虽然从技术上讲,我将其用作一个唯一表单(基本html),然后在“def post”中处理请求。post.dict(),我根据外键设置了手动验证,并将其保存在它所属的位置,但它是一个简单的表单,不是modelform或django表单


另一方面,我建议您切换到CBV,这会使您的工作更轻松,实际上FBV很乏味。

表单是否相互关联?为什么不使用javascript单独发送请求呢?它们是相关的。什么意思?
user,mentor=form.save(commit=False)
commit=False将获取一个模型对象,但它不会将其保存到db,您需要调用user.save()或mentor.save()。。无论您在这里遇到什么情况,都需要这样做。您可以使用ajax提交第二个表单,类似于此链接[并使用drf在服务器端保存对象。如果需要某种外键,您可以首先保存第一个表单,并在表单的隐藏字段中使用js设置fk。@MahsaMonavari getting page在链接上不存在
Traceback:

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "C:\Users\User\Desktop\djangoproject\django-multiple-user-types-example-master\django_school\classroom\views\teachers.py" in payment_view
  124.             user,mentor = form.save(commit=False)

File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\functional.py" in inner
  257.         return func(self._wrapped, *args)

Exception Type: TypeError at /teachers/payment_view
Exception Value: 'User' object is not iterable
class TeacherSignUpForm(UserCreationForm):
    email = forms.EmailField(max_length=100)
    first_name = forms.CharField(max_length=100)
    last_name = forms.CharField(max_length=100)
    linkedin = forms.URLField(max_length=200)
    address = forms.CharField(max_length=500)
    billing_name = forms.CharField(max_length=200)
    account_num = forms.IntegerField()
    bank_name = forms.CharField(max_length=50)
    branch_code = forms.IntegerField()

    class Meta(UserCreationForm.Meta):
        model = User
        fields = ('email', 'username', 'first_name', 'last_name')

    def save(self, commit=True):
        self.instance.is_teacher = True
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.save()
        mentor = Mentor.objects.get_or_create(
            user=user,
            linkedin=self.cleaned_data['linkedin'],
            address=self.cleaned_data['address'],
            billing_name=self.cleaned_data['billing_name'],
            account_num=self.cleaned_data['account_num'],
            bank_name=self.cleaned_data['bank_name'],
            branch_code=self.cleaned_data['branch_code'],
        )
        return user
...
class MentorPaymentForm(forms.ModelForm):
    class Meta:
        model = Mentor
        fields = ('address', 'billing_name', 'account_num', 'bank_name', 'branch_code')
def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.is_teacher = True
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']

        mentor = Mentor.objects.get_or_create(
            user=user,
            linkedin=self.cleaned_data['linkedin'],
            address=self.cleaned_data['address'],
            billing_name=self.cleaned_data['billing_name'],
            account_num=self.cleaned_data['account_num'],
            bank_name=self.cleaned_data['bank_name'],
            branch_code=self.cleaned_data['branch_code'],
        )
        if commit:
            user.save()
        return user