我有这么简单的看法:
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>
怎么了?