Skip to content

Instantly share code, notes, and snippets.

@JamesHovious
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesHovious/459fdb492944a730fe5b to your computer and use it in GitHub Desktop.
Save JamesHovious/459fdb492944a730fe5b to your computer and use it in GitHub Desktop.
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td id="first_name"></td>
<td id="last_name"></td>
<td id="date"></td>
</tr>
</tbody>
</table>
<script src="{{ STATIC_URL }}js/brython/brython.js" type="text/javascript"></script>
<body onload="brython()">
<script type="text/python">
from browser import document, ajax
#Ajax arguments
getqs = """json={"tag":"account_page","data":{"first_name": "John","last_name": "Doe","email":"useremail@email.com","password":"P@$$w0rd","date":"March 15 2015"}}"""
postqs = qs = """json={"tag":"account_page","data":{"email":"email@email.com","first_name": "firstname","last_name": "lastname","account_type":"individual",}}"""
url = 'http://[your-domain-name].com/[your-view-name]'
def post_data(url, qs):
req = ajax.ajax()
# Bind the complete State to the on_post_complete function
req.bind('complete',on_post_complete)
# send a POST request to the url
req.open('POST',url,True)
req.set_header('content-type','application/x-www-form-urlencoded')
# send data as a dictionary
req.send(qs)
def get_data(url, qs):
req = ajax.ajax()
req.bind('complete',on_get_complete)
# Bind the complete State to the on_get_complete function
req.open('GET', url+'?'+qs, True)
req.set_header('content-type', 'application/x-www-form-urlencoded')
req.send()
def on_post_complete(req):
if req.status==200 or req.status==0:
# Take our response and inject it into the html div with id='main'
document["main"].html = req.text
else:
document["main"].html = "error "+req.text
def on_get_complete(req):
if req.status==200 or req.status==0:
populate_table(req.text)
else:
document["get"].html = "error "+req.text
def populate_table(data):
data_list = data['results']
last = data_list.pop(2)
first = data_list.pop(1)
date = data_list.pop(0)
document['first_name'] <= first
document['last_name'] <= last
document['date'] <= date
get_data(url, qs)
post_data(url, qs)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment