简体   繁体   中英

How to store dynamically generated HTML form elements from Javascript in Python?

I have an HTML form that a user can add an arbitrary amount of input fields to through jQuery. The user is also able to remove any input field from any position. My current implementation is that each new input box has an id of "field[i]" so when the form is posted it is processed in Python as field1, field2 field3, ...field[n]

i = 0
while self.request.get("field" + str(i)):
        temp = self.request.get("field" + str(i))
        someList.append(temp)
        i += 1

(Assume the JavaScript handles removing of deleted elements and sorts the field names prior to post for simplicity)

This approach is working for me, but is there a better way to handle this situation? I feel like this is a very brute force method.

Platform information: Python 2.5.4; JavaScript; DHTML; jquery; Google App Engine

Edit: It appears that self.request.get_all() was the solution: GAE Doc

You could serialize the data with javascript and pass it in as json. Then you would just have a dictionary to work with in python. You would need something like simplejson , of course

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM