开发者

Ordered list form value in Django form

Is there a way to submit an unbounded list of values as part of a form and retrieve it in Django as an ordered list? I saw this question: Dj开发者_如何转开发ango equivalent of PHP's form value array/associative array which makes sense, however since the values are submitted with the same name as separate POST values I assume they are unordered.

Ideally, I'd like this on the front end:

<input type="hidden" name="list[0]" value="blah">
<input type="hidden" name="list[1]" value="blah2">
<input type="hidden" name="list[2]" value="blah3">

and be able to see which list item occurred in which position when the form was submitted.

I want it sorted in an order determined in the front-end HTML, not sorted by value. I know about getlist(), but that doesn't preserve order.


As far as I'm aware, the square bracket notation is a convention of PHP rather than HTML per se.

There seems to be a Django workaround using dot notation to generate expanded dictionaries (http://stackoverflow.com/questions/2527476/phps-form-bracket-trick-is-to-djangos). I haven't tried it, but it seems it could work

The other solution is to parse the form names yourself, extracting the number from the square brackets and generating your own ordered list

(DISCLAIMER: I'm a mere casual programmer, and am more than willing to acknowledge that im wrong - but hopefully the SO moderation system will moderate any silly comments into oblivion)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜