I have a Pyramid web app that GETs data from the user, fetches some data from the backend based on the values posted to the view, and then render the fetched results.
This is the workflow:
user->enter name->fetch age,other_details based on 'name' from DB->return a neat table with fetched values
I use ajax to do the first part. i.e., posting values from webpage to view.
Here's the relevant code to POST
<script>
var studentname=$(#stuname).val();
$.ajax({
type: "POST",
url: "/trypass",
data: {name:studentname},
cache: false,
success: function(result) {
alert("Successfully inserted!");
}
});
</script>
<input type="text" id="stuname"></input>
<div id="tablegoeshere"><!--I need fetched results here in a table.--></div>
My views that handle the posted request(Just a semifunctional try):
@view_config(route_name='try', renderer='/trypass.pt')
@view_config(route_name='tryjson',renderer='json')
def upload_view(request):
student_name = request.POST.get('name')
age=DBSession.query(Student).filter(name==student_name).first()
return {"age":age.id} #I dont need this. I need the whole tuple returned but dont know how.
You can see I have stacked a json renderer below my view decorator, but in a different route. I followed it from the documentation but This does nothing than return the values in a new route which is of no use to me.
I researched a lot but not convinced why would I want to use a json renderer to render the returned tuples; and most importantly, HOW.
What I want to know is, how/where do I pass the json values and return it within the same template(trypass.pt)? I have a dedicated to fill in with the parsed json results. But I am absolutely clueless on how to do this. Please guide me. Thank you very much in advance.
MORE EDITS:- After more research I found out that the getjson() method in javascript gets a json input and we can parse it. But my question still remains. How is the passing done? And am I AJAXing the right way? I also saw there are callbacks in AJAX which probably fetches my response and renders it to my html page. Point me in the right direction please.
Below is an example of "what I think you are asking". I tried to make it very basic so you can see what is going on. Hopefully it will get you where you need to be.
Also, once the ajax request returns with the rendered html (result['html']) you will need to insert it into the DOM.
Of course this is just one way of doing this using AJAX.
You need to study up on chameleon templating and fully understand that so you can create your table in 'student.pt'
student.pt
<table >
<tr>
<td>Student Age</td>
</tr>
<tr>
<td>${age}</td>
</tr>
</table>
test.js
$.ajax({
type: "POST",
url: "tryjson",
data: {name:studentname},
cache: false,
success: function(result) {
alert("Successfully return our ajax data and html!");
html = result['html'] #do want you want with the html here (rendered with "student_table.pt")
age = result['age'] #return this json data for example
}
});
views.py
from pyramid.renderers import render
@view_config(name='tryjson', renderer='json')
def server_view1(request):
student_name = request.POST.get('name')
age=DBSession.query(Student).filter(name==student_name).first()
template_vars = dict()
template_vars['age'] = age.id #the template will need this data so it can fill in.
result = dict()
result['html'] = render('templates/student.pt', template_vars, request)
result['age'] = age.id #Return this data for example
return result