金字塔和json。请揭秘
Pyramid and json. Please demystify
我有一个 Pyramid 网络应用程序,它从用户那里获取数据,根据发布到视图的值从后端获取一些数据,然后呈现获取的结果。
这是工作流程:
user->enter name->fetch age,other_details based on 'name' from DB->return 一个整洁的 table with fetched values
我使用 ajax 完成第一部分。即从网页发布值以供查看。
这是 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>
我处理发布请求的视图(只是半功能性尝试):
@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.
你可以看到我在我的视图装饰器下面堆叠了一个 json 渲染器,但是在不同的路径上。我从文档中遵循它,但这只是 return 对我没有用的新路由中的值。
我研究了很多但不相信为什么我要使用 json 渲染器来渲染 returned 元组;最重要的是,怎么做。
我想知道的是,how/where 我是否在同一模板 (trypass.pt) 中传递 json 值和 return 值?我有一个专门用来填写解析后的 json 结果的。但我完全不知道如何做到这一点。请指导我。非常感谢您。
更多编辑:-
经过更多研究,我发现 javascript 中的 getjson() 方法获取了 json 输入,我们可以解析它。但我的问题仍然存在。传球是如何完成的?我 AJAX 的方法正确吗?我还看到 AJAX 中有回调,可能会获取我的响应并将其呈现到我的 html 页面。请指出我正确的方向。
下面是 "what I think you are asking" 的例子。我试着让它变得非常基本,这样你就可以看到发生了什么。希望它能带你去你想去的地方。
此外,一旦 ajax 请求 returns 并呈现 html(结果['html']),您需要将其插入 DOM.
当然这只是使用 AJAX 的一种方法。
您需要研究变色龙模板并充分理解这一点,这样您才能在 'student.pt'
中创建您的 table
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
这是一种稍微不同的方法。这样只有 returns html 回到你的 ajax 而没有像其他答案那样的任何额外的 json 数据。
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(html) {
alert("Successfully return our ajax data and html!");
;now insert my html somewhere
}
});
views.py
@view_config(name='tryjson', renderer='templates/student.pt')
def server_view1(request):
student_name = request.POST.get('name')
age=DBSession.query(Student).filter(name==student_name).first()
return {'age':age.id}
我有一个 Pyramid 网络应用程序,它从用户那里获取数据,根据发布到视图的值从后端获取一些数据,然后呈现获取的结果。
这是工作流程:
user->enter name->fetch age,other_details based on 'name' from DB->return 一个整洁的 table with fetched values
我使用 ajax 完成第一部分。即从网页发布值以供查看。
这是 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>
我处理发布请求的视图(只是半功能性尝试):
@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.
你可以看到我在我的视图装饰器下面堆叠了一个 json 渲染器,但是在不同的路径上。我从文档中遵循它,但这只是 return 对我没有用的新路由中的值。
我研究了很多但不相信为什么我要使用 json 渲染器来渲染 returned 元组;最重要的是,怎么做。
我想知道的是,how/where 我是否在同一模板 (trypass.pt) 中传递 json 值和 return 值?我有一个专门用来填写解析后的 json 结果的。但我完全不知道如何做到这一点。请指导我。非常感谢您。
更多编辑:- 经过更多研究,我发现 javascript 中的 getjson() 方法获取了 json 输入,我们可以解析它。但我的问题仍然存在。传球是如何完成的?我 AJAX 的方法正确吗?我还看到 AJAX 中有回调,可能会获取我的响应并将其呈现到我的 html 页面。请指出我正确的方向。
下面是 "what I think you are asking" 的例子。我试着让它变得非常基本,这样你就可以看到发生了什么。希望它能带你去你想去的地方。
此外,一旦 ajax 请求 returns 并呈现 html(结果['html']),您需要将其插入 DOM.
当然这只是使用 AJAX 的一种方法。
您需要研究变色龙模板并充分理解这一点,这样您才能在 'student.pt'
中创建您的 tablestudent.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
这是一种稍微不同的方法。这样只有 returns html 回到你的 ajax 而没有像其他答案那样的任何额外的 json 数据。
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(html) {
alert("Successfully return our ajax data and html!");
;now insert my html somewhere
}
});
views.py
@view_config(name='tryjson', renderer='templates/student.pt')
def server_view1(request):
student_name = request.POST.get('name')
age=DBSession.query(Student).filter(name==student_name).first()
return {'age':age.id}