2

我有这么简单的看法:

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,
    })

我单击带有空标题字段的提交,并且表单集验证说它是有效的(我期望有关所需值的消息)但是如果将表单集交换为这样的表单:

ArticleFormSet = ArticleForm

在标记为:“# [1]”的行中,表单显示消息“此字段是必需的”(我期望的)

.html 文件:

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

怎么了?

4

1 回答 1

0

问题是您如何在模板中调用表单。它应该是这样的:

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

您可以在此处了解有关它们的更多信息。

于 2013-07-17T11:18:31.703 回答