简体   繁体   English

如何在同一个模型的同一个 Django 视图中保存多个表单? 当前最后一个表单在保存时覆盖

[英]How do I save multiple forms in the same Django View from the same Model? Currently the last form overwrites in save

How do I save multiple forms in the same Django View from the same Model?如何在同一个模型的同一个 Django 视图中保存多个表单?

Currently the last form overwrites in save当前最后一个表单在保存时覆盖

views.py视图.py

def createrequest_1(request):

  if request.method == 'POST':

    form1 = SurveyNameForm(request.POST)
    form2 = Question1Form(request.POST)
    form3 = Question2Form(request.POST)

    if form1.is_valid() and form2.is_valid() and form3.is_valid():
        survey = form1.save(commit=False)
        question1 = form2.save(commit=False)
        question2 = form3.save(commit=False)
        survey.survey_creation_date = datetime.date.today()
        survey.customuser = request.user
        survey.save() 
        question1.survey = survey 
        question1.save()
        question2.survey = survey 
        question2.save()

        return HttpResponseRedirect('createrequest_2')
    else:
        form = SurveyNameForm()

  return render(request, 'requestapp/createrequest_1.html', {'SurveyNameForm': SurveyNameForm, 'Question1Form':Question1Form, 'Question2Form': Question2Form})

You must set different prefix for every form.您必须为每个表单设置不同的前缀。 Put instances of forms for render in the template.将要呈现的表单实例放在模板中。

In this way:通过这种方式:

def createrequest_1(request):
    form1 = SurveyNameForm(request.POST or None, prefix='survey')
    form2 = Question1Form(request.POST or None, prefix='question1')
    form3 = Question2Form(request.POST or None, prefix='question2')

    if request.method == 'POST':

        if form1.is_valid() and form2.is_valid() and form3.is_valid():
            survey = form1.save(commit=False)
            question1 = form2.save(commit=False)
            question2 = form3.save(commit=False)
            survey.survey_creation_date = datetime.date.today()
            survey.customuser = request.user
            survey.save()
            question1.survey = survey
            question1.save()
            question2.survey = survey
            question2.save()

            return HttpResponseRedirect('createrequest_2')


    return render(request, 'requestapp/createrequest_1.html',
                  {'SurveyNameForm': form1, 'Question1Form': form2, 'Question2Form': form3})

This is explained in https://docs.djangoproject.com/en/3.0/ref/forms/api/#django.forms.Form.prefix这在https://docs.djangoproject.com/en/3.0/ref/forms/api/#django.forms.Form.prefix 中有解释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从不同页面的表单中将数据保存到同一模型中……注册多个字段? Django的 - How to save data to the same model from forms in different pages…Registration multiple fields? Django django:我如何在没有 django 表单的情况下在 django 中保存表单? - django: How do i save form in django without the django forms? Django:如何拆分表单条目并将拆分的元素保存到同一模型中, - Django: How to split a form entry and save split elements into the same model, 如何在 django 中同时保存和创建两个 Model 用户? - How do I Save and create Two Model user at same time in django? 将额外的字段保存到 django model 除了 django 表单中存在的字段(对于同一型号) - Save extra fields to django model apart from the fields which were present in django form(for the same model) CrispyForms:FormHelper-获取 </form> 在另一个地方,保存来自同一模型的两个表单 - CrispyForms: FormHelper - to get the </form> in another place, save two forms from the same model 如何在 Selenium 中保存多个同名截图? - How do I save multiple screenshots with same name in Selenium? 在Django项目中使用asteapie将多个对象保存在同一模型中 - Save multiple objects in same model using tastypie in a Django project 在 Django 中保存模型表单 - Save model forms in django Django以相同的形式保存多个数据-不同用户的不同数据 - Django save multiple data with same form - different data for different users
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM