8

So I'm almost totally new to web development as a whole, but have been thrown into a side project using Django to pull and parse data from a web service, and am struggling to understand exactly how things work, even while looking through the Django documentation.

In Django, I have everything set up and working at a basic level (using templates, a page is displayed saying "Hello World").

Now in order to pull the data from the webservice, I need to make a request to a URL of the following format:

http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]

In the provided PHP example, they do this using cURL, and then json_decode.

What would I do to get similar functionality out of Django? Thanks in advance!

Steven Mercatante
  • 24,757
  • 9
  • 65
  • 109
elykl33t
  • 887
  • 6
  • 11
  • 24

2 Answers2

12

You would use urllib2 and json standard modules (or, alternatively, the excellent library requests and json):

import urllib2
import json

url = 'http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]'
serialized_data = urllib2.urlopen(url).read()

data = json.loads(serialized_data)

 

If you want it on a page, you want it in a view, which you will need to associate with an url.

Your urls.py will contain something like

from django.conf.urls import patterns, url, include

urlpatterns = patterns('',
    (r'^get_data/$', 'myapp.views.get_data'),
)

And your myapp/views.py will contain something like

from django.http import HttpResponse
import urllib2
import json

def get_data(request):
    url = 'http://wbsapi.withings.net/[service_name]?action=[action_name]&[parameters]'
    serialized_data = urllib2.urlopen(url).read()

    data = json.loads(serialized_data)

    html = "<html><body><pre>Data: %s.</pre></body></html>" % json.dumps(data, indent=2)

    return HttpResponse(html)

Of course, I don't know how you wanted your data to be displayed, so I just serialized it again :)

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • Where would I put this within the app? Would it have its own python file? Would it go in views.py? Sorry I have so many questions! Thanks! – elykl33t Jan 03 '13 at 17:04
  • What are you trying to achieve? – Pavel Anossov Jan 03 '13 at 17:06
  • For now, very basic. I want to be able to send the request, get the json data, deserialize it, and display it on a page. – elykl33t Jan 03 '13 at 17:08
  • Thanks very much Pavel! The overall structure of the app is starting to make a lot more sense now. The only error I'm running into right now is the page says: `Using the URLconf defined in withings.urls, Django tried these URL patterns, in this order: ^get_data/$ The current URL, , didn't match any of these.` How can I fix this? – elykl33t Jan 03 '13 at 17:37
  • You're trying to request an url you didn't define. The only url available is "/get_data/". Did you forget any of the slashes? – Pavel Anossov Jan 03 '13 at 18:40
  • I ended up figuring it out. Thanks very much for the help! – elykl33t Jan 04 '13 at 15:09
  • @Pavel Anossov is urllib2.urlopen going to block all website till roundtrip of request finishes? – Kairat Kempirbaev Apr 11 '17 at 18:09
  • @PavelAnossov Thanks a lot sir :) – Ahtisham Jan 10 '18 at 12:50
0

While it would be possible to implement this sort of functionality in Django, there's a bunch of intermediary steps involved. First, you should just think about how you're going to use Python to query the webservice. For this, I would highly recommend the Requests module, which is great for this.

Benjamin White
  • 779
  • 6
  • 25