json - 在 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')
解决方案
在 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链接。
推荐阅读
- react-native - react-native-mapbox-gl 在发布模式下时应用程序崩溃
- python - 它们是一种内置方法,可直接用于使用 python-gitlab 通过名称搜索 gitlab 存储库吗
- excel - Excel VBA公式不计算
- mobx - MobX 表示 StoreProvider 组件中有一个提供的值不能转换为 observable
- structure - verilog 结构赋值模式中的冒号 (:) 表达式是什么?
- java - IDEA maven依赖包下载不了
- mysql - 带有 WHERE 条件的 MySQL 子查询
- json - JSONDecodeError:期望值:Keras+Rest API 应用程序的第 1 行第 1 列(字符 0)
- node.js - 我可以在单核系统上的子进程中 fork node.js 进程以避免阻塞事件循环
- ios - iOS 相机应用程序的 Facebook 评论