[英]how to get nearby places using google places with python and flask
我正在嘗試使用帶有python和flask的googleplaces到達附近的地方
我收到此錯誤:(分配前引用了UnboundLocalError:局部變量'place_name')
這是我的代碼:
@app.route('/Search', methods=['POST', 'GET'])
@login_required
def Search():
if request.method == 'POST':
query_result = google_places.nearby_search(
lat_lng={'lat':31.7917, 'lng' : 7.0926},
radius=500,
types=[types.TYPE_SHOPPING_MALL] or [types.TYPE_STORE])`
if query_result.has_attributions:
print(query_result.html_attributions)
for place in query_result.places:
place.get_details()
place_name = place.name
print(place.name)
place_rating = place.rating
print(place.rating)
place_location = place.get_location
print(place.get_location)
for photo in place.photos:
photo.get(maxheight=500, maxwidth=500)
photo.mimetype
photo.url
photo.filename
photo.data
return render_template('Search.html', place_name, place_rating, place_location)
else:
return render_template('Search.html')```
#Note: i am new to python in general
return render_template('Search.html', place_name, place_rating, place_location)
上面的語法無效。 將詳細信息傳遞給模板時,您需要執行以下操作:
return render_template('Search.html', name = place_name,
rating = place_rating, location = place_location)
然后,可以在模板中以{{name}}
, {{rating}}
和{{location}}
形式訪問變量name
, rating
和location
。
但是,布局for循環的方式意味着第一次到達return語句時,它將停止循環並返回包含這些變量的模板。
也許這就是您想要的,但是您可能希望將query_result
傳遞給模板,並在模板中實現Jinja2 for循環以打印出各種場所的詳細信息。 您將刪除for循環並將整個塊替換為:
return render_template('Search.html', all_places = query_result)
然后在模板中類似:
{% if all_places %}
{% for place in all_places %}
<p><b>{{place.name}}</b> has a rating of <u>{{place.rating}}</u></p>
{% endfor %}
{% else %}
<p>No places found.</p>
{% endif %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.