如何在 Python 中存储从 Javascript 动态生成的 HTML 表单元素?
我有一个 HTML 表单,用户可以通过 jQuery 添加任意数量的输入字段。用户还可以从任何位置删除任何输入字段。我当前的实现是每个新输入框的 id 为“field[i]”,因此当发布表单时,它会在 Python 中处理为 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
(假设 JavaScript 处理删除为了简单起见,删除了元素并在发布之前对字段名称进行了排序)
这种方法对我来说很有效,但是有没有更好的方法来处理这种情况?我觉得这是一个非常暴力的方法。
平台信息:Python 2.5.4; JavaScript; DHTML; jQuery; Google App Engine
编辑: self.request.get_all() 似乎是解决方案:GAE 文档
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 javascript 序列化数据并将其作为 json 传递。然后你就可以在 python 中使用字典了。当然,您需要类似 simplejson 的东西
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