简体   繁体   English

通用 FormView 中的 Django Formset

[英]Django Formset in generic FormView

Hi i used formsets in generic FormView like this:嗨,我在通用 FormView 中使用了表单集,如下所示:

class RequestRecommendationView(FormView):
    template_name = "account/stepfour.html"
    form_class = formset_factory(StepFourForm)

    def form_valid(self,form):
        print form.is_valid()
        cleaned_data = form.cleaned_data
        # return redirect(reverse("some_url_name"))

form for formset_factory is like this: formset_factory 的表单是这样的:

class StepFourForm(forms.Form):
    contact_person = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    company = forms.CharField(required=True)
    request_message = forms.CharField(required=True)

my html structure is like this:我的 html 结构是这样的:

<div style="padding-top:100px;padding-left:10px;">
    <h4> Request a Recommendation </h4>
    <form method="post" action="">
            {% csrf_token %}

            <table id="myForm">
                <tbody>
                {% for f in form %}
                    {% for field in f %}
                        <tr>
                            <td>{{field.label_tag}}</td>
                            <td>{{field}}</td>

                            {% for error in field.errors %}
                            <td><span>{{ error }}</span></td>
                            {% endfor %}
                        </tr>
                    {% endfor %}
                {% endfor %}
                </tbody>
            </table>

            <button class="btn btn-primary btn-xlarge" type="submit" name="submit">Request Now</button>
            {{ form.management_form }}
        </form>
</div>

Then i used django-dynamic-formset ( https://code.google.com/p/django-dynamic-formset/ ) to add/remove extra forms through these:然后我使用 django-dynamic-formset ( https://code.google.com/p/django-dynamic-formset/ ) 通过这些添加/删除额外的表单:

<script type="text/javascript">
    $(function() {
               $('#myForm tbody').formset();
           })
</script>

the problem is: if i leave the form empty,(every field is required), it manages to get to form_valid() of my class-view (though it should not), if i fill one of the fields (leaving others unfilled), it displays the errors associated message successfully.问题是:如果我将表单留空(每个字段都是必需的),它会设法到达我的类视图的 form_valid() (尽管它不应该),如果我填写其中一个字段(让其他字段未填写) ,它会成功显示错误相关消息。 why is this happening ?为什么会这样? what should i do to fix it ?我该怎么做才能解决它? is providing form_class a formset is behind all of this ?是否提供 form_class 一个表单集是所有这一切的幕后推手?

This question has been asked 6 years ago, I found it because i faced the same issue, since it is unanswered, I'll provide the solution i found.这个问题是 6 年前被问到的,我发现它是因为我遇到了同样的问题,因为它没有得到解答,我将提供我找到的解决方案。 according to the documentation to validate a minimum of forms, you have to provide a min_num and set validate_min=True in the formset_factory:根据验证最少表单的文档,您必须提供一个min_num并在min_num设置validate_min=True

formset = formset_factory(your_form, min_num=2, validate_min=True, extra=2)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM