使用Python Web GET数据
我正在尝试通过 url 将信息传递到 python 页面。 我有以下链接文本:
"<a href='complete?id=%s'>" % (str(r[0]))
在完整页面上,我有这个:
import cgi
def complete():
form = cgi.FieldStorage()
db = MySQLdb.connect(user="", passwd="", db="todo")
c = db.cursor()
c.execute("delete from tasks where id =" + str(form["id"]))
return "<html><center>Task completed! Click <a href='/chris'>here</a> to go back!</center></html>"
问题是,当我进入完整页面时,我在“id”上收到关键错误。 有谁知道如何解决这一问题?
编辑
当我运行 cgi.test()
时,它没有给我任何东西,
我认为我使用 url 的方式有问题,因为它没有被传递。
它基本上 localhost/chris/complete?id=1
/chris/ 是一个文件夹,complete 是 index.py 中的一个函数
我是否正在格式化网址错误?
I'm trying to pass information to a python page via the url. I have the following link text:
"<a href='complete?id=%s'>" % (str(r[0]))
on the complete page, I have this:
import cgi
def complete():
form = cgi.FieldStorage()
db = MySQLdb.connect(user="", passwd="", db="todo")
c = db.cursor()
c.execute("delete from tasks where id =" + str(form["id"]))
return "<html><center>Task completed! Click <a href='/chris'>here</a> to go back!</center></html>"
The problem is that when i go to the complete page, i get a key error on "id". Does anyone know how to fix this?
EDIT
when i run cgi.test()
it gives me nothing
I think something is wrong with the way i'm using the url because its not getting passed through.
its basically localhost/chris/complete?id=1
/chris/ is a folder and complete is a function within index.py
Am i formatting the url the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该错误意味着
form["id"]
未能在cgi.FieldStorage()
中找到键"id"
。要测试调用的 URL 中包含哪些键,请使用 cgi.test():
编辑:基本测试脚本(使用带有 Linux 路径的 python cgi 模块)只有 3 行。 确保您知道如何在系统上运行它,然后从浏览器调用它以检查 CGI 端是否可以看到参数。 您可能还想使用
import cgitb 添加 回溯格式 ; cgitb.enable()。
The error means that
form["id"]
failed to find the key"id"
incgi.FieldStorage()
.To test what keys are in the called URL, use cgi.test():
EDIT: a basic test script (using the python cgi module with Linux path) is only 3 lines. Make sure you know how to run it on your system, then call it from a browser to check arguments are seen on the CGI side. You may also want to add traceback formatting with
import cgitb; cgitb.enable()
.您是否尝试过打印出表单的值以确保您得到了您认为得到的结果? 不过,您的代码确实有一些问题...您应该执行 form["id"].value 从 FieldStorage 获取项目的值。 另一种选择是自己动手,如下所示:
这应该会导致如下结果:
Have you tried printing out the value of form to make sure you're getting what you think you're getting? You do have a little problem with your code though... you should be doing form["id"].value to get the value of the item from FieldStorage. Another alternative is to just do it yourself, like so:
This should result in something like this:
首先,您应该通过进行字典查找,
因为如果键不在字典中,这会将 None 分配给变量。 然后,您可以使用该
习惯用法(是的,我通常喜欢空检查和防御性编程......)。 python 文档也提出了类似的建议。
在不深入研究代码的情况下,我认为您应该引用
以提取您似乎要求的数据。
First off, you should make dictionary lookups via
Because this assigns None to the variable, if the key is not in the dict. You can then use the
idiom (yes, I'm a fan of null checks and defensive programming in general...). The python documentation suggests something along these lines as well.
Without digging into the code too much, I think you should reference
in order to extract the data you seem to be asking for.