我构建了一个小网页,从阅读器获取 RFID ID 并检查用户是否已经存在,如果存在,则将他们重定向到特定页面。在我的笔记本电脑上一切正常,但自从我将其移至 RPi 后,我收到了一个奇怪的 500 错误。
终端消息应该是什么样的:
starting
{'MemberID': u'7A0092B6F9A7'}
Member ID 7A0092B6F9A7
{"MemberExists":true,"MemberName":"WORK","State":"Logged In"}
True
Noooooo
127.0.0.1 - - [12/Aug/2015 10:52:00] "POST /index HTTP/1.1" 200 -
Sending confirmation for check in
127.0.0.1 - - [12/Aug/2015 10:52:00] "GET /getSignIn HTTP/1.1" 200 -
It worked
127.0.0.1 - - [12/Aug/2015 10:52:00] "GET /activity HTTP/1.1" 200 -
实际情况:
starting
{'MemberID': u'7A0092B6F9A7'}
Member ID 7A0092B6F9A7
{"MemberExists":true,"MemberName":"WORK","State":"Logged Out"}
127.0.0.1 - - [12/Aug/2015 16:02:12] "POST /index HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/share/pyshared/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/share/pyshared/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/share/pyshared/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/usr/share/pyshared/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/share/pyshared/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/share/pyshared/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/pi/connectedLogin/app/views.py", line 64, in index
isMember = q.json()["MemberExists"]
TypeError: 'dict' object is not callable
Sending confirmation for check in
127.0.0.1 - - [12/Aug/2015 16:02:12] "GET /getSignIn HTTP/1.1" 200 -
It worked
127.0.0.1 - - [12/Aug/2015 16:02:12] "GET /signup HTTP/1.1" 200 -
这是我的 Flask 代码:
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
global checkCheck
global userIdentifier
print "starting"
location = {'mspace': 'Central Library'}
if request.method == 'POST':
global userIdentifier
global isMember
global memberID
checkCheck = True
member = {'MemberID': request.form['cardID']}
memberID = request.form['cardID']
print member
print "Member ID " + memberID
q = requests.post("https://external_DB/login.json", data=member)
print q.text
isMember = q.json()["MemberExists"]
print isMember
userIdentifier = q.json()["MemberName"]
if isMember == False:
#do stuff for a new member
print "YEAH"
print userIdentifier
else:
#do stuff for a recurring member
print "Noooooo"
return render_template('index.html',
location = location,
user = member
)
还有我的 html:
<html>
<head>
<title>{{ location['mspace'] }} - Makerspace </title>
</head>
<body>
<div id="banner">
<img src="{{url_for('static', filename='bubbler.jpeg')}}" style="width:100%"></img>
</div>
<div style="text-align: center; display: table; width: 100%; height: 77%">
<h1 style="display: table-cell; vertical-align: middle">Welcome to {{ location['mspace'] }} - Place your card on the reader to begin</h1>
</div>
</body>
<script>
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send(null);
return xmlHttp.responseText;
}
var refreshIntervalId = setInterval(function(){checkFunc()}, 1000);
function checkFunc(){
var json = httpGet("/getSignIn");
console.log("yes!");
obj = JSON.parse(json);
console.log("new checkin = "+obj.newCheckin);
console.log("MemberID = "+obj.MemberID);
console.log("IsMember? "+obj.isMember);
if(obj.newCheckin){
clearInterval(refreshIntervalId);
checkUser(obj.isMember, obj.memberID);
}
}
function checkUser(isMember, memberID){
if(isMember == false){
window.location = "signup";
}
else{
window.location = "activity";
}
}
</script>
</html>
最佳答案
您正在使用旧版本的requests
。在 1.0.0 版之前,response.json
属性是一个属性,直接返回 JSON 对象。如果您使用的是基于 Debian 或 Ubuntu 的 Linux 发行版,那么您可能安装了 0.8.4 版。
要么将 requests
库升级到更新的版本(您的版本已经过时将近 3 年),要么删除 ()
调用:
isMember = q.json["MemberExists"]
userIdentifier = q.json["MemberName"]
关于python - 在 RaspberryPi 上使用 Flask 应用程序出现 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31970699/