Bläddra i källkod

Adding form to yelp API example

David Leonard 10 år sedan
förälder
incheckning
0cfd9259b4

+ 2 - 2
hackathon_starter/hackathon/scripts/yelp.py

@@ -8,8 +8,8 @@ CONSUMER_SECRET = 'VCK-4cDjtQ9Ra4HC5ltClNiJFXs'
 TOKEN = 'AWYVs7Vim7mwYyT1BLJA2xhNTs_vXLYS'
 TOKEN_SECRET = 'Rv4GrlYxYGhxUs14s0VBfk7JLJY'
 
-def requestData():
-    url = 'http://api.yelp.com/v2/business/yelp-san-francisco?'
+def requestData(location):
+    url = 'http://api.yelp.com/v2/business/' + location
 
     consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
     oauth_request = oauth2.Request(method="GET", url=url)

+ 1 - 0
hackathon_starter/hackathon/templates/hackathon/api_examples.html

@@ -17,6 +17,7 @@
 		<div class="col-sm-4"><a href="http://localhost:8000/hackathon/quandlstocks/">Quandl Example</a></div>
 		<div class="col-sm-4"><a href="http://localhost:8000/hackathon/meetupUser/">Meetup</a></div>
 		<div class="col-sm-4"><a href="http://localhost:8000/hackathon/yelp/">Yelp</a></div>
+		<div class="col-sm-4"><a href="http://127.0.0.1:8000/hackathon/nytimesarticles/">New York Times</a></div>
 
   	</div>
 

+ 29 - 1
hackathon_starter/hackathon/templates/hackathon/yelp.html

@@ -1 +1,29 @@
-{{ data }} 
+<!DOCTYPE html>
+<html>
+    {% include 'hackathon/base.html' %}
+    <style>
+      .form-signin {
+        max-width: 550px;
+        padding: 15px;
+        margin: 0 auto;
+      }    
+    </style>
+
+    <body>
+
+      {{ data }}
+      <h1 class="text-center"> Yelp API </h1>
+      <div class="container text-center">
+        <form class="form-signin" id="login_form" method="post" action="/hackathon/yelp/">
+          {% csrf_token %}
+          {% load bootstrap %}
+          <br>
+          <input type="text" name="location" class="form-control" placeholder="Enter a location" value="" required autofocus>
+          <br>
+          <button class="btn btn-lg btn-primary btn-block" type="submit">Search</button>
+          <input type="hidden" name="submit" value="submit" />
+        </form>
+      </div>
+
+    </body>
+</html>

+ 4 - 1
hackathon_starter/hackathon/views.py

@@ -147,7 +147,10 @@ def facebook(request):
 #################
 
 def yelp(request):
-    data = requestData()
+    data = {}
+    if request.method == 'POST':
+        location = request.POST.get('location')
+        data = requestData(location)
     return render(request, 'hackathon/yelp.html', { 'data': data })
 
 #################