通用FormView中的Django Formset

时间:2013-09-13 08:58:33

标签: python django formset

您好我在通用FormView中使用了formset,如下所示:

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"))

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)

我的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>

然后我使用了django-dynamic-formset(https://code.google.com/p/django-dynamic-formset/) 通过以下方式添加/删除额外的表单:

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

问题是: 如果我将表单留空,(每个字段都是必需的),它设法获取我的类视图的form_valid()(尽管它不应该),如果我填充其中一个字段(留下其他字段未填充),它会显示错误关联消息成功。为什么会这样?我该怎么做才能解决这个问题?提供form_class一个formset背后的所有这些?

1 个答案:

答案 0 :(得分:0)

这个问题是在6年前提出的,我找到它是因为我遇到了同样的问题,由于未得到回答,我将提供我找到的解决方案。 根据文档以验证最少的表单,您必须提供2020-05-10 16:10:10.504 INFO 10000 --- [ctor-http-nio-2] GET all items from server : onSubscribe(MonoFlatMapMany.FlatMapManyMain) 2020-05-10 16:10:10.504 INFO 10000 --- [ctor-http-nio-2] GET all items from server : request(unbounded) 2020-05-10 16:10:10.511 INFO 10000 --- [ctor-http-nio-8] GET all items from server : onNext(Item(id=5eb7f9461a10790e4902ac1a, description=bla bla, price=4999.0)) 2020-05-10 16:10:10.512 INFO 10000 --- [ctor-http-nio-8] GET all items from server : onNext(Item(id=5eb7f9461a10790e4902ac1c, description=bla bla bla, price=7249.99)) 2020-05-10 16:10:10.512 INFO 10000 --- [ctor-http-nio-8] GET all items from server : onNext(Item(id=5eb7f9461a10790e4902ac1b, description=bla bla bla bla, price=2399.99)) 2020-05-10 16:10:10.512 INFO 10000 --- [ctor-http-nio-8] GET all items from server : onNext(Item(id=5eb7f9461a10790e4902ac1d, description=bla bla bla bla bla, price=699.99)) 2020-05-10 16:10:10.512 INFO 10000 --- [ctor-http-nio-8] GET all items from server : onNext(Item(id=5eb7f9461a10790e4902ac1e, description=another item, price=1.99)) 2020-05-10 16:10:10.513 INFO 10000 --- [ctor-http-nio-8] GET all items from server : onComplete() 并在formset_factory中设置min_num

validate_min=True