首页 > 解决方案 > 在 Flask Web 应用程序中,如何使用搜索框过滤器显示 json 文件数据?

问题描述

有人可以帮帮我吗 ?如果他在搜索框中按卡车 ID 搜索,我想在网页上向用户显示表格中的卡车 ID、名称等数据。

搜索框的 HTML 代码是:

<form method="GET">
              <div class="form-row">
                <div class="col-12 col-md-9 mb-2 mb-md-0">
                  <input type="text" class="form-control form-control-lg" placeholder="Enter truck-id">
                </div>
                <div class="col-12 col-md-3">
                  <button type="submit" class="btn btn-block btn-lg btn-primary">Search</button>
                </div>
              </div>
            </form>

示例 Json 文件是:

{

"status": "ok",
"totalResults": 6,
"Fleet": [{
        "Truck": {
            "id": "12345GHJ",
            "name": "OilTank"
        },
        "driver": "Sarah driverlady",
        "summary": "Deliver oil to businesses in town abcd",
        "description": "Deliver oil to businesses in town abcd drive from office to this town before 01 January and return before 10 January. More detailed notes about the job are added here",
        "DateJobSubmitted": "2020-07-13T18:15:00Z"
    },
    {
        "Truck": {
            "id": "789456GGY",
            "name": "Frozen Food"
        },
        "driver": "Mike Andrews",
        "summary": "Deliver frozen food to businesses in town abcd",
        "description": "Deliver oil to businesses in town abcd drive from office to this town before 01 January and return before 10 January. More detailed notes about the job are added here",
        "DateJobSubmitted": "2020-07-13T18:15:00Z"
    }
]

}

The py file code for this part is :

with open('samplefile.json', 'r') as m : 
    data = json.load(m) 

app = Flask(__name__)
@app.route("/truck_id",methods=["GET","POST"])
def truck_id():
    return render_template('truck_id.html')

标签: jsonflaskweb-applicationspython-requests

解决方案


在 HTML 文件中,为输入标签指定一个“名称”。在 python 文件中,在 truck_id() 下使用 request 命令获取信息。通过解析来检查文本是否在子文件中。如果存在,则与 render_template 一起发送状态。

这是一个示例,请相应地进行更改:

HTML 文件:

<form method="GET">
          <div class="form-row">
            <div class="col-12 col-md-9 mb-2 mb-md-0">
              <input type="text" class="form-control form-control-lg" placeholder="Enter truck-id" name = 'truck_id'>
            </div>
            <div class="col-12 col-md-3">
              <button type="submit" class="btn btn-block btn-lg btn-primary">Search</button>
            </div>
          </div>
</form>

{% if status == "found" %}
<p>The items is found </p>
{% endif %}
{% if status == "not found" %}
<p>The items is not found </p>
{% endif %}

蟒蛇文件:

with open('samplefile.json', 'r') as m : 
data = json.load(m) 

app = Flask(__name__)
@app.route("/truck_id",methods=["GET","POST"])
def truck_id():
    status = ""
    details = request.form
    truck_id = details['truck_id']
    if truck_id in json:
       //do the JSON parsing here
       if found:
          status = "found"
       else:
          status = "not found"
    return render_template('truck_id.html',status = status)

所以,这样你就可以检查了!特此附上我执行基本 CRUD 操作的GitHub链接。


推荐阅读