在应用引擎上序列化表单提交值

0 投票
1 回答
2641 浏览
提问于 2025-04-17 09:43

我只是想把提交的表单里的所有字段都转成一种可以保存的格式,就像这样在PHP里:

json_encode($_GET)

json.dumps(self.request.get) 这个方法不管用:

<type 'exceptions.TypeError'>: <bound method Request.get of <Request at 77ea190 GET http://localhost:8083/?a=value>> is not JSON serializable 
      args = ('<bound method Request.get of <Request at 77ea190...ocalhost:8083/?a=value>> is not JSON serializable',) 
      message = '<bound method Request.get of <Request at 77ea190...ocalhost:8083/?a=value>> is not JSON serializable'

我试过用CGI模块,但也出现了不能转化的错误。如果你在想我为什么要这样做:我用这个方法是为了把数据发送回我的模板,以便重新填充表单字段。

1 个回答

5

self.request.get 是用来获取请求中的数据的方法,但它返回的是这个方法本身,而不是你想要的数据。你需要这样做:

json.dumps(self.request.GET.items())

request.GET 会返回一个叫做 UnicodeMultiDict 的对象,而 request.GET.items() 会返回一个包含多个元组的列表,每个元组的格式是 (key, value)

可以参考这两个链接了解更多信息:http://code.google.com/appengine/docs/python/tools/webapp/requestclass.htmlhttp://docs.webob.org/en/latest/reference.html#id1

撰写回答