我有可以找到并打印最近医院的代码:
python
:
@app.route('/check', methods=['POST'])
def check():
place = 'Checking nearest hospitals..'
API_KEY = 'MY_KEY'
google_places = GooglePlaces(API_KEY)
query_result = google_places.nearby_search(
lat_lng ={'lat': 40.4222532, 'lng': 49.8035278},
radius = 5000,
types =[types.TYPE_HOSPITAL])
if query_result.has_attributions:
print (query_result.html_attributions)
for place in query_result.places:
print(place.name)
return render_template('check.html', place=place.name)
default.html
:
<form action="/check" method="POST">
<button type="check" class="btn btn-primary">Nearest Hospitals</button>
</form>
hospital.html
<div class="box">
<p>{{ place.name }}</p>
</div>
问题是,我在html框中只能看到一所医院,但是在控制台中有许多医院打印。如何像控制台一样在框中打印每个医院?
您需要发送整个查询,而不只是地名
python:
return render_template('hospital.html', places=query_result.places)
您可以将它们全部打印在循环中
hospital.html
<div class="box">
{% for place in places %}
<p>{{ place.name }}</p>
{% endfor %}
</div>