Django formset with crispy - 提交按钮不提交

Django formset with crispy - submit buttons not submitting

我正在使用带有 crispy 的 FormSet,每行只有一个提交按钮,但是点击提交目前不会更新记录。 我已经搜索并找到了一些类似的答案,这些答案表明提交不在表单内,但我的是。另外缺少一个表单操作,但是 none 我的其他脆皮表单有操作并且它们可以正常工作。

从下面的代码中可以看出是否还有其他原因导致记录无法保存?

forms.py

class ChangeGroupForm(FormHelper):
    def __init__(self, *args, **kwargs):
        super(ChangeGroupForm, self).__init__(*args, **kwargs)
        self.form_method = 'post'
        self.css_class = 'form-inline'
        self.form_id = 'changegroup_form'
        self.form_show_labels = False 
        self.layout = Layout(
            Div(
                Div(                          
                    Div(
                        Field('group', placeholder='Group', css_class="form-control mb-2 mr-sm-2"),
                    css_class='col-lg-3'
                    ),    
                    Div(
                        Field('gps', placeholder='gps coords', css_class="form-control mb-2 mr-sm-2"),
                    css_class='col-lg-8'
                    ), 
                    Div(
                        HTML("""<input type="submit" name="submit" value="Save" class="btn btn-primary mt-1"/>"""),
                    css_class='col-lg-1'
                    ),               
                css_class='row'
                ),
            )
        )
        self.render_required_fields = True

views.py

@login_required
def db_change_groups(request):
    change_form = modelformset_factory(ChangeGroup, fields=('group','gps'))
    change_form_helper = ChangeGroupForm()

    return render(request, 'home/db_change_groups.html', {
        "change_form": change_form,
        "change_form_helper": change_form_helper,
        }) 

template.html

{% crispy change_form change_form_helper %}

已呈现 html

<form id="changegroup_form" method="post"> <input type="hidden" name="csrfmiddlewaretoken"
        value="7v0000CPl3G70M6HLfF2FAiwefdfsdgdfwewdf7Gp4nay1hFqZ1Y34SBUA000mHBZQ54">
    <div> <input type="hidden" name="form-TOTAL_FORMS" value="10" id="id_form-TOTAL_FORMS"> <input type="hidden"
            name="form-INITIAL_FORMS" value="9" id="id_form-INITIAL_FORMS"> <input type="hidden"
            name="form-MIN_NUM_FORMS" value="0" id="id_form-MIN_NUM_FORMS"> <input type="hidden"
            name="form-MAX_NUM_FORMS" value="1000" id="id_form-MAX_NUM_FORMS"> </div>
    <div>
        <div class="row">
            <div class="col-lg-3">
                <div id="div_id_form-0-group" class="form-group">
                    <div class="controls "> <input type="text" name="form-0-group" value="A" maxlength="50"
                            class="form-control mb-2 mr-sm-2 textinput textInput form-control" placeholder="Group"
                            id="id_form-0-group"> </div>
                </div>
            </div>
            <div class="col-lg-8">
                <div id="div_id_form-0-gps" class="form-group">
                    <div class="controls "> <input type="text" name="form-0-gps" maxlength="255"
                            class="form-control mb-2 mr-sm-2 textinput textInput form-control" placeholder="gps coords"
                            id="id_form-0-gps"> </div>
                </div>
            </div>
            <div class="col-lg-1"> <input type="submit" name="submit" value="Save" class="btn btn-primary mt-1" />
            </div>

        </div>

    </div> <input type="hidden" name="form-0-id" value="1" id="id_form-0-id">
    <div>
        <div class="row">
            <div class="col-lg-3">
                <div id="div_id_form-1-group" class="form-group">
                    <div class="controls "> <input type="text" name="form-1-group" value="B" maxlength="50"
                            class="form-control mb-2 mr-sm-2 textinput textInput form-control" placeholder="Group"
                            id="id_form-1-group"> </div>
                </div>
            </div>
            <div class="col-lg-8">
                <div id="div_id_form-1-gps" class="form-group">
                    <div class="controls "> <input type="text" name="form-1-gps" maxlength="255"
                            class="form-control mb-2 mr-sm-2 textinput textInput form-control" placeholder="gps coords"
                            id="id_form-1-gps"> </div>
                </div>
            </div>
            <div class="col-lg-1"> <input type="submit" name="submit" value="Save" class="btn btn-primary mt-1" />
            </div>

        </div>

    </div> 

    </div> <input type="hidden" name="form-9-id" id="id_form-9-id">
</form>

您需要将这样的东西附加到您的视图中

def db_change_groups(request):
    ....
    if request.method == "POST":
        form = ChangeGroupForm(request.POST)
        if form.is_valid():
            # Access cleaned data with
            group = form.cleaned_data['group']
            # Then you can save this to a model.

            # return success template or something
        else:
            # Check for errors. 

如果您使用 ModelForm 而不是 FormHelper 创建表单,则可以使用 form.save() 自动将其保存到模型中。