mercredi 22 avril 2015

Searching with a defined query in Django / Python

Currently as it stands I have a search bar and a button on my homepage like 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>

I have created a file which is within my websites folder which looks like so.

import urllib2
import json

locu_api = '17501ab24118c5cd0981b6070543d972cb2052d1'

def loc_search(query):
    api_key = locu_api
    url = '' + 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']

Essentially what I am trying to do is when the user search's for a city e.g. New York then the loc_search(query) is called populating the search field '(query)' with the users input and the data then being displayed.

I haven't much experience with either django or api's so any help would be invaluable. Thanks again

Aucun commentaire:

Enregistrer un commentaire