Display JSON in Javascript from Python Bottle -
i trying access mongodb record within javascript function display document on webpage. using bottle framework pymongo, have tried first encode mongodb document json object pass javascript function.
@bottle.route('/view/<_id>', method = 'get') def show_invoice(_id): client = pymongo.mongoclient("mongodb://localhost") db = client.orders collection = db.myorders bson.objectid import objectid result = collection.find_one({'_id': objectid(_id)}) temp = json.dumps(result,default=json_util.default) print "temp: " + temp return bottle.template('invoice', rows = temp)
when try display document within html page javascript function, nothing happens. however, when call variable, rows, trying pass {{rows}} within body of html display. seems js function not display anything.
<!doctype html> <html> <head> <head> <title>invoice report</title> <script type="text/javascript"> function filltable() { var obj = {{rows}}; document.write(obj); } </script> </head> </head> <body onload="filltable()"> <div class="invoice"> </div> <h4>rows body</h4> {{rows}} </body> </html>
i tried use jquery deserialize json object rows function
jquery.parsejson(rows);
and jquery.parsejson({{rows}});
i tried make variable unescaped everywhere possible {{!rows}} see doing wrong? how take mongodb document pymongo, , use bottle display on webpage? realize similar questions have been asked, can't seem have found work in particular situation.
the issue isn't bottle rendering json, it's using document.write()
.
open new tab in browser, , point url: 'about:blank'. give blank webpage. now, right click , open developer tools. try running document.write('stuff');
context. shouldn't see changes page.
instead try:
var body = document.getelementsbytagname("body")[0]; body.innerhtml = "stuff";
and note difference.
there of course, many other ways achieve effect, simplest without requirements on external javascript libraries.
Comments
Post a Comment