如何在python中显示同一页面上显示的db.query搜索结果

2024-09-29 19:18:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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

<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,我想在同一页面中显示该记录的详细信息


Tags: nameformidhtml记录详细信息button页面
1条回答
网友
1楼 · 发布于 2024-09-29 19:18:40

这里有一个可能的解决方案

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.
       });
    });
});

相关问题 更多 >

    热门问题