我是一个蟒蛇新手。我试图使用python来查询数据库。我需要将一些参数传递给python页面,以便它可以查询数据库。但我在检索帖子变量方面遇到了麻烦。
HTML:
<select class="chooseRace" id="race">
<option value="president">Presidential</option>
<option value="governor">Gubernatorial</option>
<option value="senate">Senatorial</option>
</select>
jquery的:
var e = document.getElementById("race");
var strRace = e.options[e.selectedIndex].value;
$.ajax({
url: "http://localhost/py/getRaces.py",
method: "POST",
data: {level: strLevel},
success: function(data) {
// Successful POST; do something with the response if you want
console.log("ok, it worked. ");
},
error: function(jxhr, statusText, err) {
// Error, handle it
console.log(statusText + " " + err);
}
});
蟒:
import MySQLdb
import cgitb
cgitb.enable()
import json
import requests
print "Content-Type: text/html" # HTML is following
print
print request.POST['level'] # for POST form method
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="user", # your username
passwd="pass", # your password
db="my_dbs") # name of the data base
cur = db.cursor()
cur.execute("SELECT * FROM mytable")
但是当我运行localhost / py / getRaces.py时,我得到了这个:
<type 'exceptions.NameError'> Python 2.7.8: C:\Python27\python.exe
Tue Jul 22 14:58:21 2014
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
C:\code\getRaces.py in ()
7 print "Content-Type: text/html" # HTML is following
8 print
=> 9 print request.POST['level'] # for POST form method
10
11 db = MySQLdb.connect(host="localhost", # your host, usually localhost
request undefined
<type 'exceptions.NameError'>: name 'request' is not defined
args = ("name 'request' is not defined",)
message = "name 'request' is not defined"
任何人都知道我做错了什么?我以为我安装了请求库。
答案 0 :(得分:0)
不应该是以下内容:
print requests.post['level'] # for POST form method
错误消息表明您的程序中没有“请求”这样的内容。
答案 1 :(得分:0)
您使用的是哪个框架?
您正在尝试访问应保存已接收数据的对象,但它未在功能范围内定义或在您的框架中根本不存在。