0

i am trying to get nearby places using googleplaces with python and flask

i am getting this error: (UnboundLocalError: local variable 'place_name' referenced before assignment)

here is my code:

@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
yas17sin
  • 11
  • 2
  • I am guessing the error is due to code that is not shown maybe, have check of this thread: https://stackoverflow.com/questions/370357/python-variable-scope-error – ilamaaa Jan 25 '19 at 16:05
  • 1
    hmm i tried your result but it didn't work, anyhow i have gotten an answer by @25 thansks for responding tho. – yas17sin Jan 25 '19 at 16:33

1 Answers1

1
return render_template('Search.html', place_name, place_rating, place_location)

The above isn't valid syntax. When you pass the details to the template, you need to do it as:

return render_template('Search.html', name = place_name,
                        rating = place_rating, location = place_location)

The variables name, rating and location will then be accessible in the template as {{name}}, {{rating}} and {{location}}.

However, the way you have the for loops laid out means the first time the return statement is reached, it will stop the loop and return the template with these variables.

Perhaps this is what you want, but you may wish to pass query_result to the template, and implement a Jinja2 for loop in the template to print out the various place details. You would remove the for loops and replace that whole block with:

return render_template('Search.html', all_places = query_result)

Then in the template something like:

{% 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 %}
v25
  • 7,096
  • 2
  • 20
  • 36
  • 1
    thank you so much, you fixed my problem now the thing is i tried too many times so i can't know for sure if i am returning any results because of OVER_QUERY_LIMIT error. – yas17sin Jan 25 '19 at 16:32
  • Looking at your sample code, I'm assuming you're using `https://github.com/slimkrazy/python-google-places` which appears to be quite out of date. You may wish to switch to the offical version at https://github.com/googlemaps/google-maps-services-python. – v25 Jan 25 '19 at 17:02
  • 1
    hmmm i didn't know that there was a updated version thanks for letting me know i'll take a look and change my code right away. – yas17sin Jan 25 '19 at 17:19
  • 1
    okay so i tried implementing the 'official version' i have gotten this code but it didn't work: https://pastebin.com/MSrS2Mvg – yas17sin Jan 25 '19 at 20:00
  • Please spin up a separate question, as this is a slightly different issue. – v25 Jan 25 '19 at 20:20
  • 1
    :/ i can't post a new question because someone didn't like my title and gave minus rep which blockes me from asking questions for 1 day, and i am in need of a solution so bad. – yas17sin Jan 25 '19 at 21:01