On Thursday, February 5, 2015 at 11:10:32 AM UTC-8, Henry Versemann wrote:
>
> I have a django view (django v. 1.7 ; python v. 2.7.8) which currently and 
> successfully sends a request to an api, and receives a good response back. 
> I'm sending and receiving using the 'requests' library (v. 2.4.3). Now in 
> addition to sending the raw response data back in an HttpResponse I would 
> also like to strip out some of the data and create a summary report of it, 
> to also send back in my HttpResponse.
>
What Larry said - you need to convert the javascript data format into a 
python data format. json.loads() will do that for you. Here's a working 
example: 


import json

def get_nyt_news():
    # NYT API data
    response = requests.get(
        
'http://api.nytimes.com/svc/mostpopular/v2/mostshared/all-sections/7.json?api-key={apikey}'.format(apikey=settings.NYT_API_KEY)
        )

    json_data = json.loads(response.text)
    data = json_data['results'][0:4]

    # Rewrite data to conform internal links
    for d in data:
        d['get_absolute_url'] = d['url']

    return {'object_list': data}

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b7300a03-2200-485f-9d4e-227f08cef92d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to