我正在使用 Django 1.4,但我正在为 Formsets 苦苦挣扎。我已经阅读了文档和很多 SO 问题。我对它们的实际工作方式一无所知。
- Fomset 应该像这样配置/声明吗?
我是如何在 forms.py 和 views.py 中设置表单集的。
forms.py:(我已经声明了一个forms.Form)
class BicycleAdItemKindPropertyValueForm(forms.Form):
bicycleaditemkindpropertyvalue = forms.ChoiceField()
视图.py:
def submit_ad_view(request):
# This class is used to make empty formset forms required
class BicycleAdItemKindPropertyValueFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(BicycleAdItemKindPropertyValueFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
def clean(self):
pdb.set_trace()
for form in self.forms:
None
#form.fields['bicycleaditemkindpropertyvalue']
formsetBicycleAdItemKindPropertyValue = formset_factory(BicycleAdItemKindPropertyValueForm, formset=BicycleAdItemKindPropertyValueFormSet)
if request.method == 'POST':
model_main = Main()
model_main.section = Section.objects.get(pk=request.POST['section'])
model_main.user = request.user
model_bicyclead = BicycleAd()
model_bicyclead.bicycleadtype = BicycleAdType.objects.get(pk=2) #1-Ad Link, 2-Ad Insite. Em principio ficara pre-definido
model_bicyclead.bicycleaditemkind = BicycleAdItemKind.objects.get(pk=4)
model_bicyclead.bicycleadcondition = BicycleAdCondition.objects.get(pk=2)
model_bicyclead.city = GeonamesLocal.objects.get(pk=4803854)
# Build the forms
form_main = MainForm(request.POST, instance = model_main)
form_bicyclead = BicycleAdForm(request.POST, instance = model_bicyclead)
form_bicycleadcategory = BicycleAdCategoryForm(request.POST)
form_bicycleaditemkindselect = BicycleAdItemKindSelectForm(request.POST)
formset_bicycleaditemkindpropertyvalue = formsetBicycleAdItemKindPropertyValue(request.POST)
#pdb.set_trace()
if form_main.is_valid() and form_bicyclead.is_valid() and form_bicycleadcategory.is_valid() and form_bicycleaditemkindselect.is_valid() and formset_bicycleaditemkindpropertyvalue.is_valid():
main_f = form_main.save()
bicyclead_f = form_bicyclead.save(commit=False)
bicyclead_f.main = main_f
bicyclead_f.save()
bicycleadcategory_f = form_bicycleadcategory.save(commit=False, rel_obj=model_bicyclead)
else:
# Build the forms
form_main = MainForm()
form_bicyclead = BicycleAdForm()
form_bicycleadcategory = BicycleAdCategoryForm()
form_bicycleaditemkindselect = BicycleAdItemKindSelectForm()
formset_bicycleaditemkindpropertyvalue = formsetBicycleAdItemKindPropertyValue()
return render_to_response('app/submit/submit_ad.html', {'form_main': form_main, 'form_bicyclead': form_bicyclead, 'form_bicycleadcategory': form_bicycleadcategory, 'form_bicycleaditemkindselect': form_bicycleaditemkindselect, 'formset_bicycleaditemkindpropertyvalue': formset_bicycleaditemkindpropertyvalue}, context_instance=RequestContext(request))
前面这两个步骤正确吗?表单集声明正确?
现在,模板。我已经这样声明了:
一些模板.html:
{{formset_bicycleaditemkindpropertyvalue.management_form}}
{{formset_bicycleaditemkindpropertyvalue.non_form_errors}}
{{formset_bicycleaditemkindpropertyvalue.errors}}
{% for form in formset_bicycleaditemkindpropertyvalue.forms %}
<div class="item-bicycleaditemkindpropertyvalue">
<label for="{{form.bicycleaditemkindpropertyvalue.auto_id}}"></label>
{{form.bicycleaditemkindpropertyvalue}}
</div>
在 JavaScript 的帮助下,代码将生成如下内容:
<form action="/submeter/anuncio/" method="post"><div style="display: none;"><input type="hidden" value="3TWjKpqnXG7afZcvF1YmmWcRBKnYqePB" name="csrfmiddlewaretoken"></div>
...
<div class="someclass">
<input type="hidden" id="id_form-TOTAL_FORMS" value="1" name="form-TOTAL_FORMS">
<input type="hidden" id="id_form-INITIAL_FORMS" value="0" name="form-INITIAL_FORMS">
<input type="hidden" id="id_form-MAX_NUM_FORMS" name="form-MAX_NUM_FORMS">
[]
<div class="item-bicycleaditemkindpropertyvalue">
<label for="id_form-0-bicycleaditemkindpropertyvalue">Some 0</label>
<select id="id_form-0-bicycleaditemkindpropertyvalue" name="form-0-bicycleaditemkindpropertyvalue">
...
</select>
</div><div class="item-bicycleaditemkindpropertyvalue">
<label for="id_form-1-bicycleaditemkindpropertyvalue">Some 1</label>
<select id="id_form-1-bicycleaditemkindpropertyvalue" name="form-1-bicycleaditemkindpropertyvalue">
...
</select>
</div>
<div class="item-bicycleaditemkindpropertyvalue">
<label for="id_form-2-bicycleaditemkindpropertyvalue">Some 2</label>
<select id="id_form-2-bicycleaditemkindpropertyvalue" name="form-2-bicycleaditemkindpropertyvalue">
...
</select>
</div>
</div>
<input type="submit">
</form>
输出正确吗?应该是这样的吗?
- 验证,如何验证表单集?
现在关于验证。我的表单中有一个 forms.ChoiceField()
class BicycleAdItemKindPropertyValueForm(forms.Form):
bicycleaditemkindpropertyvalue = forms.ChoiceField()
我读过应根据选择列表验证 forms.ChoiceField() 。我的问题是我该怎么做?
- 一些信息调试
在 clean() 中使用 pdb(python debuger) 我看到我得到了这些数据:
class BicycleAdItemKindPropertyValueFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(BicycleAdItemKindPropertyValueFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
def clean(self):
pdb.set_trace()
for form in self.forms:
None
#form.fields['bicycleaditemkindpropertyvalue']
调试 self.data:
(Pdb) self.data
<QueryDict: {u'form-1-bicycleaditemkindpropertyvalue': [u'29'],
u'form-2-bicycleaditemkindpropertyvalue': [u'33'],
u'title': [u'adasd'],
u'form-MAX_NUM_FORMS': [u''],
u'country': [u'42'],
u'section': [u'5'],
u'item_kind': [u'4'],
u'form-INITIAL_FORMS': [u'0'],
u'bicycleadcategorytype': [u'3', u'2'],
u'csrfmiddlewaretoken': [u'3TWjKpqnXG7afZcvF1YmmWcRBKnYqePB'],
u'form-TOTAL_FORMS': [u'3'],
u'form-0-bicycleaditemkindpropertyvalue': [u'4']}>
调试 self.is_valid():
(Pdb) self.is_valid()
False
关于如何验证此表单集的任何线索?
最好的祝福,