How to manage post method request in Django -
i cannot right data following post form:
view.py
queryset=examplet.objects.filter(id=var_id).order_by('value1') form = hiddenform(initial={'ids': [o.id o in queryset]}) return render(request, 'test.html', {'form': form})
form.py
class hiddenform(forms.form): ids = forms.multiplechoicefield(widget=forms.multiplehiddeninput())
output:
<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" />
this value of request.post:
<querydict: {u'csrfmiddlewaretoken': [u'g2ktgfpda5aa3bk2baaes2naexxkkh4y'], u'ids': [u'1', u'2', u'10']}>
how can within array 3 values, possibly in order [10, 1, 2]?
i have tried code, return 1 value:
var_for=request.post.get('ids') tmp_var in range(len(var_for)): print var_for[tmp_var]
always use form.cleaned_data
, check form.is_valid()
def myview(request): if request.method == 'post': # if form has been submitted... form = hiddenform(request.post) # form bound post data if form.is_valid(): # validation rules pass # process data in form.cleaned_data # ... ids = form.cleaned_data['ids'] # ... return sort of http response else: queryset=examplet.objects.filter(id=var_id).order_by('value1') form = hiddenform(initial={'ids': [o.id o in queryset]}) return render(request, 'test.html', {'form': form})
Comments
Post a Comment