[Answer]-Json query on button press python / django

1👍

First of all, your html should put in a form with a ‘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 you need a urls.py file contains:

url(r"^api/", 'loc_search', name="search")

And in views you can do like 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)

    for item in data['objects']:
        print item['name']
        print item['phone']

Leave a comment