gpt4 book ai didi

Django Formsets - 如何正确构建表单集并对其进行验证?

转载 作者:行者123 更新时间:2023-12-03 17:54:04 26 4
gpt4 key购买 nike

我正在使用 Django 1.4 并且我正在为 Formsets 苦苦挣扎。我已经阅读了文档和很多 SO 问题。我越来越不知道它们是如何工作的。

- Fomsets 应该像这样配置/声明吗?

我是如何在 forms.py 和 views.py 中设置表单集的。

forms.py:(我已经声明了一个forms.Form)

class BicycleAdItemKindPropertyValueForm(forms.Form):
bicycleaditemkindpropertyvalue = forms.ChoiceField()

View .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))

前面这两个步骤对吗?表单集是否正确声明?

现在,模板。我是这样声明的:

sometemplate.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

关于如何验证此表单集的任何线索?

此致,

最佳答案

formset在上面的代码中声明正确。此外,生成的输出是表单集生成它的理想方式。关于验证表单,你可以简单地做。

for form in formset:
if form.is_valid():
# If is valid do something here
else:
# Do something Else

如果你想自己做验证的东西,你可以覆盖 clean form下的方法类,因为此方法由 is_valid 调用方法。
class BicycleAdItemKindPropertyValueForm(forms.Form):
bicycleaditemkindpropertyvalue = forms.ChoiceField()


def clean(self):
# Do field validation here.

如果您想验证并且只有一个字段,您可以覆盖 clean_field方法如下:
class BicycleAdItemKindPropertyValueForm(forms.Form):
bicycleaditemkindpropertyvalue = forms.ChoiceField()

def clean_bicycleaditemkindpropertyvalue(self):
# Do single field validation here.

关于Django Formsets - 如何正确构建表单集并对其进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14280875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com