def search_handler(response): context = {} entry = response.get_field("search") lat_field = response.get_field("latitude") long_field = response.get_field("longitude") if lat_field and long_field: lat = float(lat_field) long = float(long_field) context["user_loc"] = [lat, long] else: context["user_loc"] = None tags = response.get_field("tags") if entry: entry = entry.strip() context["query"] = entry context["tags"] = tags if tags == "": search_results = Location.search_name(entry) elif entry == "": search_results = Location.search_tag(tags) else: search_results = Location.search(tags, entry) if lat_field and long_field: search_results.sort(key=lambda x: x.distance_from(lat, long)) context["results"] = search_results render_page("searchresult.html", response, context)