【问题标题】:Pyramid and json. Please demystify金字塔和json。请揭秘
【发布时间】:2015-05-20 23:40:59
【问题描述】:

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

这是工作流程:

user->enter name->fetch age,other_details based on 'name' from 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 页面。请指出我正确的方向。

【问题讨论】:

  • 您能澄清一下您真正想要返回浏览器的内容吗? Json 数据?,来自模板的 html?,两者都有?
  • html表中的json数据..
  • 所以你最终想要填充数据的html?有几种方法可以做到这一点,所以你必须非常清楚。您可以只返回 html (.pt),也可以返回实际的 json 数据,并将呈现的 html 嵌入到 json 数据中。它们是两种不同的东西。
  • 我有一个返回/呈现 JSON 数据的视图,我只想将 json 数据制表。或者,我听到了这个新选项。在 HTML 中嵌入 json 数据?请大神指教一下怎么做?
  • 我也可以有一个获取表单来输入用户名并以 json 格式获取其余详细信息,但我不确定如何实现它。请帮忙

标签: python ajax json pyramid templating


【解决方案1】:

这是一种稍微不同的方法。这种方式只会将 html 返回到您的 ajax,而无需像其他答案那样添加任何额外的 json 数据。

学生.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}

【讨论】:

    【解决方案2】:

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

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

    当然,这只是使用 AJAX 的一种方式。

    您需要学习变色龙模板并充分理解这一点,这样您才能在“student.pt”中创建表格

    学生.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
    

    【讨论】:

    • 我在下面为您添加了另一个答案,它只返回模板 html。我还更改了 student.pt,所以它是一个非常基本的表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 2021-03-21
    • 2014-07-02
    • 2011-06-19
    相关资源
    最近更新 更多