首页 > 解决方案 > 如何在 python 的同一页面上显示显示的 db.query 搜索结果

问题描述

我有一个 html 页面(searchCustomer.html),它有一个输入字段,我们在其中输入要在数据库中搜索的字符串,在我们单击搜索按钮后成功获取记录并以 html 显示。我想显示从 html 中单击的记录的详细信息,当在同一页面(searchCustomer.html)中单击第一列(即 href)时

<form action="/getsearchresults" method="post">
          <!-- <form action="/getsearchresults" method=  methods=['GET', 'POST'] > -->
          <Text  text="Customer Name" id="text0"/>
          <Input width="200px"  name="custName"  required  placeholder="Please enter the customer name"/>
          <button type="submit">Search Customer</button>
          <button  id="#btnID">TEst Customer</button>


</form>

<table border="1">
    <tr><td><strong>Customer Id</strong></td><td><strong>Name</strong></td></tr>
     {% for name1 in customers %}
     <tr><td><a href="/DispCustomerInfo?query={{ name1.name }}">{{name1.id}}</a></td><td>{{name1.name}} 
      </td></tr>
     {% endfor %}
</table>

单击href,我想在同一页面中显示该记录的详细信息

标签: pythonflask

解决方案


这是一个可能的解决方案

from flask import request, jsonify

@app.route('/DispCustomerInfo')
def get_customer_info():
    name = request.args.get('query', False)
    customer = {}
    if name:
        #1 Query database
        #2 Assign result to customer
    return jsonify(customer)

在前端Ajax用于获取客户数据。

<td><a class="customername" href="#" data-name="{{ name1.name }}">{{name1.id}}</a></td>
$( document ).ready(function() {
    $(".customername").on('click', function(){
       var name = $(this).data('name');
       $.get("/getReport", {'query': name}).done(function (data) {
           // Use jquery to append customer data to a DOM element.
       });
    });
});


推荐阅读