Issue with passing JSON to a view in Web2py

Go To


Wrote a very simple function to pull data from the espn api and display in default/index. However default/index is a blank page.

At this point I'm not even trying to parse through the JSON - I just want to see something on my browser.

import urllib2
import json

#espn_uri being pulled from models/

def index():
    r = urllib2.Request(espn_uri) 
    opener = urllib2.build_opener()    
    f =
    status = json.load(f)

    return dict(status)



Thank you!

2012-04-03 22:39
by user1311549
Fixed it: Very basic mistake on my part - used to django notation, Web2py requires an '=' in the view

{{=statuses} - user1311549 2012-04-04 01:15


Try: return dict(status=status)

return dict(status) works because status it itself a dict, and dict(status) just copies it. But it's probably got no key named status, or at least nothing interesting.

And yes, you need =.

2012-04-04 01:41
by JLundell


As JLundell advises, first return paired data via the dictionary:

return dict(my_status=status)

Second, as you've worked out, use the following to access the returned, rather than the local variable in index.html. Make sure you use the equals sign here or nothing will display


When it comes to JSON, you can return the data using

return my_status.json()

Several other options are available to return data as a list, or to return HTML.

Finally, I recommend that you make use of jQuery and AJAX ($.ajax), so that the AJAX return value can be easily assigned to a JS object. This will also allow you to handle success or errors in the form of JS functions.

2014-11-09 01:56
by Ro Mc