我无法从以下POST表单中获取正确的数据:
view.py
queryset=ExampleT.objects.filter(id=var_id).order_by('value1')
form = HiddenForm(initial={'ids': [o.id for o in queryset]})
return render(request, 'test.html', {'form': form})
form.py
class HiddenForm(forms.Form):
ids = forms.MultipleChoiceField(widget=forms.MultipleHiddenInput())
输出:
<input id="id_ids_0" name="ids" type="hidden" value="10" />
<input id="id_ids_1" name="ids" type="hidden" value="1" />
<input id="id_ids_2" name="ids" type="hidden" value="2" />
这是request.POST:
的值<QueryDict: {u'csrfmiddlewaretoken': [u'g2ktgFpDA5aa3BK2Baaes2nAexxKKh4Y'], u'ids': [u'1', u'2', u'10']}>
如何在数组中获得3个值,可能是[10,1,2]的顺序?
我尝试过这段代码,但只返回一个值:
var_for=request.POST.get('ids')
for tmp_var in range(len(var_for)):
print var_for[tmp_var]
答案 0 :(得分:0)
答案 1 :(得分:0)
始终使用form.cleaned_data
并检查form.is_valid()
def myview(request):
if request.method == 'POST': # If the form has been submitted...
form = HiddenForm(request.POST) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
# Process the data in form.cleaned_data
# ...
ids = form.cleaned_data['ids']
# ... return some sort of http response
else:
queryset=ExampleT.objects.filter(id=var_id).order_by('value1')
form = HiddenForm(initial={'ids': [o.id for o in queryset]})
return render(request, 'test.html', {'form': form})