我正在尝试弄清楚如何将 ajax 数据发送到我的 Flask Controller ,
- 没有ajax,只需查找即可工作:
这是我的 html 模板中的表单:
<form method=post action="{{ url_for( 'editreals', id=realist.index(row), name='head') }}">
在我的 view.py 中,获取索引“id”并进行转换:
i = int(request.args.get("id")
通过 ajax,我使用 jquery 插件 ( http://www.appelsiini.net/projects/jeditable ) 进行就地编辑:
<script> $(document).ready(function() { $(".rubric").editable('{{ url_for('editskills') }}',{ submit: 'Ok', cancel: 'Cancel', tooltip : 'Clic to edit', type : 'textarea', name : 'rubric' }); }); </script> <h3 class="rubric", id={{ sklist.index(row) }}>{{ row["rubric"] }}</h3>
作为返回,我收到了 500 错误
我不明白为什么 Controller 总是发回该错误:
TypeError: list indices must be integers, not NoneTypeDataType :
从网络调试器中,我发送的数据对我来说似乎没问题:
FormData :
rubric: datasubmitted
id: 0
那些字段只是字符串,不是吗?为什么 python 说它们是 NoneTypeDataType ?
[编辑1]
标题:
Request URL:http://<Server-IPAddress>:5000/editskills
Request Method:POST
Status Code:500 INTERNAL SERVER ERROR
Request Headersview source
Accept:text/html, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:25
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:session=eyJsb2dnZWRfaW4iOnRydWV9.BcMSfg.j5SaVklwzSzTdttCciYL_9dqZNg
DNT:1
Host:<Server-IPAddress>:5000
Origin:http://<Server-IPAddress>:5000
Referer:http://<Server-IPAddress>:5000/competences
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
rubric:1Réseaux
id:0
Response Headersview source
Connection:close
Content-Type:text/html; charset=utf-8
Date:Thu, 23 Jan 2014 20:52:19 GMT
Server:Werkzeug/0.9.4 Python/2.7.3
X-XSS-Protection:0
预览:
File "/home/noa/virtualenv/folio/foliodev/portfolio/views.py", line 39, in editskills
v = liste[i]
TypeError: list indices must be integers, not NoneType
抱歉,我犯了一个错误,导致错误的代码是:
i = request.args.get("id", type=int)
v = liste[i]
如果我尝试这样做:
i = int(request.args.get("id"))
它直接中断到类型转换:
i = int(request.args.get("id"))
TypeError: int() argument must be a string or a number, not 'NoneType'
这让我关注为什么我发送的数据是“NoneType”
非常感谢您抽出时间。
最佳答案
问题是,在您的工作 form
示例中,您在查询字符串中发送 id
(这使得它可用于 request.args
)。 jEditable 在响应 body 中发送 id
作为 POST
的一部分 - Flask 在 request.form
下公开此数据>。将 i = int(request.args.get("id"))
更改为 i = int(request.form.get("id"))
一切都会正常.
关于jquery - 使用 Ajax 将数据发布到 Flask 时的 Python NoneTypeDataType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21295428/