Re: How to call url and get json data

2013-10-21 Thread Sanjay Bhangar
Mahantesh,

On Mon, Oct 21, 2013 at 3:48 PM, Mahantesh U  wrote:
> Hi,
>
>How to call the below url in django in views file:
>
>
>
> http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=O-lzp4Hyqp8=json
>
>   The above api returns json data which further i need to use and fill those
> in html file.
>
>
> HttpResponse('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=O-lzp4Hyqp8=json')
>
>  Is this correct way to call to get json data?
>

No.

Think about what you need to do - you need to fetch this JSON in your
view, process it, and then send those variables as "context" to your
template to be able to render them.

So, first, you need to fetch the JSON data in your python view. This
can be accomplished with something like:

import urllib2
json_string = 
urllib2.urlopen('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=O-lzp4Hyqp8=json').read()

^ this will store the results of that webpage into a variable called
"json_string". You can also look at the "requests" library to fetch
data from URLs in python, but urllib2 should be fine as well.

Then, we need to convert the json_string into a python dict:

import json
data = json.loads(json_string)

Now you will have a python dict with all the data in a variable called "data"

Now you can process this data, add other context data that you need,
etc. and finally render the template with something like:

from django.shortcuts import render
return render(request, "template_name.html", data)

Then in your template you can output like {{ author_name }}, {{
author_url }}, etc. as needed.

Hope that makes sense.
All the best,
Sanjay


> Thanks in advance
>
>
>
>
>
>
> --
> 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 django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> 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/10216c68-f54f-4ea9-9e9c-6d0d03227bc0%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/CAG3W7ZFrBP8cuLxupFArEt1bXT7urb%3DNqtrFVpcxGsWev46-AA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


How to call url and get json data

2013-10-21 Thread Mahantesh U
Hi,

   How to call the below url in django in views file:


  
 
http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=O-lzp4Hyqp8=json

  The above api returns json data which further i need to use and fill 
those in html file.
  
  
HttpResponse('http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=O-lzp4Hyqp8=json')

 Is this correct way to call to get json data?

Thanks in advance






-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/10216c68-f54f-4ea9-9e9c-6d0d03227bc0%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.