django的formset.is_valid()是否有效?

时间:2013-07-17 09:24:42

标签: django validation formset

我有这么简单的看法:

import datetime
from django import forms
from django.forms.formsets import formset_factory
from django.http import HttpResponse
from django.shortcuts import redirect, render, render_to_response
from django.views.generic.detail import DetailView

from form_utils.forms import BetterForm

from fb_app import models as fb_model
from learn_forms import models as lf_model

class ArticleForm(forms.Form):
    title = forms.CharField(required=True)
    #pub_date = forms.DateField()


def formset(request):
    print request.POST
    ArticleFormSet = formset_factory(ArticleForm)  # [1]
    if request.method == 'POST':
        formset = ArticleFormSet(request.POST)
        print formset.is_valid()
        if formset.is_valid():
            # do something with the formset.cleaned_data
            pass
    else:
        formset = ArticleFormSet()
        print formset.is_valid()
    return render(request, 'formset.html', {
        'formset': formset,
    })

我点击提交时使用空标题字段,并且formset验证表明它有效(我希望有关所需值的消息) 但是如果交换formset的形式如下:

ArticleFormSet = ArticleForm

在标有以下标题的行:“#[1]”表单显示消息'此字段为必填'(我期待)

html文件:

<form method="post" action="/lf/formset">
    {% csrf_token %}
    <table>
        {{ formset }}
    </table>
    <input type="submit" name="submit" >
</form>

怎么了?

1 个答案:

答案 0 :(得分:0)

问题是如何调用模板内的表单。这就是它应该是这样的:

<form method="post" action="/lf/formset">
    <table>
        {% csrf_token %}
        {{ formset.as_p }}
    </table>
    <input type="submit" name="submit" >
</form>

您可以详细了解他们here