Json query on button press python / django -
currently stands have search bar , button on homepage so.
<div class="input-group"> <input id="address" type="textbox" placeholder="city or zipcode" class="form-control"> <span class="input-group-btn"> <button class="btn btn-default" type="button" id="addresssearch">search</button> </span> </div>
i have created api.py file within websites folder looks so.
import urllib2 import json locu_api = '****' def loc_search(query): api_key = locu_api url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key locality = query.replace(' ', '%20') final_url = url + "&locality=" + locality + "&category=restaurant" json_obj = urllib2.urlopen(final_url) data = json.load(json_obj) item in data['objects']: print item['name'] print item['phone']
essentially trying when user search's city e.g. new york loc_search(query) called populating search field '(query)' users input , data being displayed.
i haven't experience either django or api's invaluable. again
first of all, html should put in form
'action':
<form action="{% url "search" %}"> <div class="input-group"> <input name="query" id="address" type="textbox" placeholder="city or zipcode" class="form-control"> <span class="input-group-btn"> <button class="btn btn-default" type="button" id="addresssearch">search</button> </span> </div> </form>
then need urls.py
file contains:
url(r"^api/", 'loc_search', name="search")
and in views can this:
import urllib2 import json locu_api = '****' def loc_search(request): query = request.get.get('query') api_key = locu_api url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key locality = query.replace(' ', '%20') final_url = url + "&locality=" + locality + "&category=restaurant" json_obj = urllib2.urlopen(final_url) data = json.load(json_obj) item in data['objects']: print item['name'] print item['phone']