I want to create a drop-down list in the input html, and the data in this drop-down list can reference my database json. What code should I write in my py file?
This is my input html. Although I can select it on the web page, it is too troublesome to fill it in one by one.
<tr><td>起始机场: </td><td><input type="text" name="user_placeofdeparture" placeholder="请输入机场名称" <input list="airport">
<datalist id="airport">
<option value="北京西郊机场">
<option value="内蒙古赤峰机场">
<option value="长治王村机场">
<option value="鄂尔多斯伊金霍洛机场">
</datalist></td></tr>
<tr><td>目的机场: </td><td><input type="text" name="user_destination" placeholder="请输入机场名称" <input list="airport">
<datalist id="airport">
<option value="北京首都国际机场">
<option value="北京西郊机场">
<option value="内蒙古赤峰机场">
<option value="长治王村机场">
<option value="鄂尔多斯伊金霍洛机场">
</datalist>
</td></tr>
This is my py file. I have originally put the airport names in this txt, but my json also has this, and this json will be used to calculate longitude and latitude in the future.
I'm a little confused, I don't know how to use it. Pleading for help.
Intercept part of the content in the json document
{"北京首都国际机场": { "latitude":40.08010101, "longitude":116.5849991},"北京西郊机场": { "latitude":39.96080017, "longitude":116.2570038}, "内蒙古赤峰机场": { "latitude":42.23500061, "longitude":118.9079971},"长治王村机场": { "latitude":36.24750137, "longitude":113.1259995},"鄂尔多斯伊金霍洛机场": { "latitude":39.49, "longitude":109.8613889},"大同机场": { "latitude":40.06029892, "longitude":113.4820023}}
Please help.
習慣沉默2017-06-14 10:54:22
First convert json to dict in python, and then take out the airport name:
import json
json_str = '{"北京首都国际机场": { "latitude":40.08010101, "longitude":116.5849991},"北京西郊机场": { "latitude":39.96080017, "longitude":116.2570038}, "内蒙古赤峰机场": { "latitude":42.23500061, "longitude":118.9079971},"长治王村机场": { "latitude":36.24750137, "longitude":113.1259995},"鄂尔多斯伊金霍洛机场": { "latitude":39.49, "longitude":109.8613889},"大同机场": { "latitude":40.06029892, "longitude":113.4820023}}'
airport_names = json.loads(json_str).keys()
// 然后把airport_names传给模板
Then in the html file:
<datalist id="airport">
{% for airport_name in airport_names %}
<option value="{{ airport_name }}">
{% endfor %}
</datalist>