金字塔和json。请神秘化

时间:2015-03-18 09:04:44

标签: python ajax json pyramid templating

我有一个Pyramid网络应用程序,它从用户那里获取数据,根据发布到视图的值从后端获取一些数据,然后渲染获取的结果。

这是工作流程:

user->输入name-> fetch age,other_details基于' name'从DB->返回一个带有取值的整洁表

我使用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渲染器,但是在不同的路径中。我从文档中跟着它,但这没有什么比在新路由中返回值对我没有用。

我研究了很多,但不相信为什么我要使用json渲染器渲染返回的元组;最重要的是,如何。

我想知道的是,我如何/在哪里传递json值并在同一模板中返回它(trypass.pt)?我有一个专门用来填写解析的json结果。但我对如何做到这一点绝对毫无头绪。请指导我。非常感谢你提前。

更多编辑: 经过更多的研究,我发现javascript中的getjson()方法获得了一个json输入,我们可以解析它。但我的问题仍然存在。传球怎么办?我是AJAXing正确的方式吗?我还看到AJAX中有回调可能会取回我的响应并将其呈现给我的html页面。请指出正确的方向。

2 个答案:

答案 0 :(得分:2)

这是一种略有不同的做法。这种方式只会将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}

答案 1 :(得分:0)

下面是“我认为你在问什么”的例子。我试着让它变得非常基本,这样你就可以看到发生了什么。希望它能让你到达你需要的地方。

此外,一旦ajax请求返回渲染的html(result ['html']),您将需要将其插入DOM。

当然,这只是使用AJAX实现此目的的一种方式。

你需要研究变色龙模板并完全理解,这样你就可以在'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