如何同时提交表单和表单集

Posted

技术标签:

【中文标题】如何同时提交表单和表单集【英文标题】:how to submit a form and formset at the same time 【发布时间】:2013-02-16 04:55:16 【问题描述】:

我正在尝试一次呈现一个表单和一个表单集。 表单集工作正常(我认为),但表单没有验证(好像没有发布数据)

我尝试过使用按钮,但它的主要提交功能来自 js。

这些表格都是独立工作的,但在提交时不能独立工作,所以看起来问题出在视图中 这是代码:

views.py

from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect
from forms import LessonForm, AddMaterialForm
from models import Lesson, SUBJECT_OPTIONS, Materials, MATERIAL_TYPES
from django.forms.formsets import formset_factory

def Create_Lesson(request):
    AddMaterials=formset_factory(AddMaterialForm, extra=9)
    if request.method == "POST": # If the form has been submitted...
        lesson = LessonForm(request.POST, prefix="lesson") # A form bound to the POST data
        formset = AddMaterials(request.POST, request.FILES) # A form bound to the POST data
        if lesson.is_valid() and formset.is_valid(): # All validation rules pass
            lesson = lesson.save(commit=False)
            lesson.creator = request.user
            lesson.save()
            for form in formset:
                form = form.save(commit=False)
                form.lesson = lesson.pk
                form.save()
            return render(request, 'index.html',)
    else:
        lesson= LessonForm(prefix='lesson') # An unbound form
        formset = AddMaterials()
    return render(request, 'create_lesson/create.html', 
    'form': lesson,'formset':formset
)

.html

    <form id="create_lesson_form" method="post" action="">
    <h2>1: Create Your Lesson</h2>

         form.non_field_errors 
        <label for="subject"><span>Subject</span></label>
        form.subject
         form.subject.errors 
        <label for="title"><span>Title</span></label>
        <input type="text" id="title" name="name" placeholder="Give it a name"/>
         form.name.errors 
        <label class="error" for="title" id="title_error">You must choose a title!</label>            
        <label for="subtitle"><span>Subtitle</span></label>
        <input type="text" id="subtitle" name="subtitle" placeholder="Type your subtitle here"/>
         form.subtitle.errors 
        <label class="error" for="subtitle" id="subtitle_error">are you sure you want to leave subtititle blank?</label>
        <label for="description"><span>Description</span></label>
        <textarea id="description" name= "description" cols="42" rows="5" placeholder="why is it important? this can be a longer description"'></textarea>
         form.description.errors 
        <label class="error" for="description" id="description_error">are you sure you want to leave the description blank?</label>
        <label for="success" id="Goals_title"><span>Goals</span></label>
        <textarea id="success" name="success" cols="42" rows="5" placeholder="explain what sucess might look like for someone doing this lesson...what does mastery look like?" '></textarea>
         form.success.errors 
        <label class="error" for="success" id="success_error">are you sure you want to leave the goals blank?</label>
     form.directions.errors 
        <label class="error" for="directions" id="directions_error">are you sure you do not want to include dierections?</label>
    <label for="directions" id="Directions_title"><span>Directions</span></label>
        <textarea id="directions" name="directions" cols="42" rows="5" placeholder="you can add simple directions here" '></textarea><br>
    </form>


    <form id="add_elements_form" method="post" action="">
    % csrf_token %
     formset.as_p
    <button type='submit' id='finish'>Finish Editing Lesson</button>
    </form>

【问题讨论】:

【参考方案1】:

这将同时提交表单和表单集。

//When your uploading files or images don't forget to put "multipart/form-data" 
//   in your form. 
//To connect formset in your form, don't forget to put the model in the formset 
//   for instance.
//In this you can add many lines as you want or delete it.

forms.py

class LessonForm(forms.ModelForm):
    class Meta:
        model = Lesson


MaterialsFormset = inlineformset_factory(Lesson, Materials, 
    fields=('field_name', 'field_name'), can_delete=True)

views.py

def view_name(request):
    form = LessonForm()
    formset = MaterialsFormset(instance=Lesson())
    if request.method == 'POST':
        form = LessonForm(request.POST)
        if form.is_valid():
            lesson = form.save()
            formset = MaterialsFormset(request.POST, request.FILES,
                instance=lesson)
            if formset.is_valid():
                formset.save()
                return render(request, 'index.html',)
    return render(request, "page.html", 
        'form': form, 'formset': formset
    )

html

<form method="post" enctype="multipart/form-data">
    % csrf_token %
     form.as_p 
     formset.as_p 
    <input type="submit" value="Save"/>
</form>

【讨论】:

【参考方案2】:

您只需要一个表单标签。如果您希望同时接收所有数据,则需要用一个表单标签包装所有字段。

【讨论】:

我只是尝试过,但它仍然没有工作......下一个答案中的方法似乎......但是谢谢 您仍然不需要两个单独的
标签。这是完全没有必要的。如果不发布您的模型,就不可能说您的表单上是否有正确的编码。如果这就是问题所在,您应该修改您的问题以更准确。如果您注意到,您接受的答案仅使用一个表单标签。
谢谢......你是对的,我已经解决了这个问题......但这还不够,我认为主要问题是我没有包括...... enctype="multipart/form-data"

以上是关于如何同时提交表单和表单集的主要内容,如果未能解决你的问题,请参考以下文章

表单_post提交方式和get的区别,元素集

表单同时提交多条记录的技术实现

表单提交和Ajax同时使用onsubmit?

如何在仍然运行 HTML 验证(最小长度、最大长度、模式...)的同时使用 JavaScript 提交表单

js 提交的表单后如何在本页面接收返回值

在 XMPP 中获得异常未授权(401),同时提交表单