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!

2 Answers 2

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 :)

Sign up to request clarification or add additional context in comments.

7 Comments

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!
What are you trying to achieve?
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.
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?
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?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.