Reputation: 785
I've build a small webpage that grabs an RFID ID from a reader and checks if the user already exists and if so redirects them to a specific page. On my laptop everything is working fine, but since I've moved it over to the RPi I'm getting a weird 500 error.
What the terminal messages should look like:
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 -
What it actually looks like:
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 -
Here's my Flask code:
@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
)
And my 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>
Upvotes: 1
Views: 339
Reputation: 1121148
You are using an older version of requests
. Until version 1.0.0, the response.json
attribute is a property returning the JSON object directly. If you are using a Linux distribution based on Debian or Ubuntu then you probably have version 0.8.4 installed.
Either upgrade the requests
library to a newer version (your version is nearly 3 years out of date), or remove the ()
call:
isMember = q.json["MemberExists"]
userIdentifier = q.json["MemberName"]
Upvotes: 3
Reputation: 12213
Here's the core of the error message:
File "/home/pi/connectedLogin/app/views.py", line 64, in index
isMember = q.json()["MemberExists"]
TypeError: 'dict' object is not callable
That means the value of q.json
is a 'dict' object. That line of code expects it to be a method that can be called.
I am unfamiliar with flask, but familiar with python. Either q.json needs to be redefined as a method, or that line of code needs to be changed so it doesn't call it. Eg isMember = q.json["MemberExists"]
Upvotes: 1