在Django模板上访问json值

问题描述:

我终于有了一种方法来使用django-allauth访问facebook上的各种值。我面临的唯一问题是访问模板上的值。在Django模板上访问json值

这里是views.py

from allauth.socialaccount.models import SocialToken 

import json 
import requests 


def fb_personality_traits(request): 
    access_token = SocialToken.objects.get(account__user=request.user, account__provider='facebook') 
    # print access_token.token 
    requested_data = requests.get(
     'https://graph.facebook.com/me?access_token=' + access_token.token + '&fields=id,name,email,posts,about') 
    data_FB = json.loads(requested_data) 
    return render(request, 'home/facebook_personality_traits.html', {'fb': data_FB}) 

这里是taht我使用的显示值模板:

<html> 


<body> 

Welcome back {{ user.name }} 
{{fb.name}} 
<!-- <img src="" height="60" width="60"> --> 


<a href="/">Home</a> 



</body> 


</html> 

我收到以下错误:

请让我知道要改进什么。

FB - ERROR

**在文本错误*​​

TypeError at /facebook_personality_traits/ 
expected string or buffer 
Request Method: GET 
Request URL: http://website:port/facebook_personality_traits/ 
Django Version: 1.11.5 
Exception Type: TypeError 
Exception Value:  
expected string or buffer 
Exception Location: /usr/lib/python2.7/json/decoder.py in decode, line 364 
Python Executable: /usr/bin/python 
Python Version: 2.7.12 
Python Path:  
['/home/ubuntu/PersonalityWithFacebook', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages'] 
Server time: Wed, 11 Oct 2017 11:00:14 +0000 

这是我在JSON变量存储JSON:gist of the json

+1

* i.stack.imgur.com目前无法处理此请求。*更之所以错误应该被添加作为文本的一部分,而不是图像。 –

+0

@MosesKoledoye等一下我加入... –

requests.get()返回response对象(的表示HTTP响应),而不是一个字符串,所以显然json.loads()崩溃了。你想要的这里是:

response = requests.get(...) 
fb_data = json.loads(response.text) 

现在requests是足够聪明,有json()方法如果响应具有“应用/ JSON的”内容类型会照顾json.loads()部分的,所以你可以使用它而不是:

response = requests.get(...) 
fb_data = response.json() 

这是说,你不应该盲目地假设你的请求成功 - 你可以有在传输层的错误(网络/ DNS /等),或者你可以有一个403,404,500或无论什么响应,所以你必须处理所有关于requests.get()调用的错误情况。

作为最后一个音符:使用字符串格式化而不是字符串拼接 - 它使代码更可读和可维护:

url = 'https://graph.facebook.com/me?access_token={token}&fields=id,name,email,posts,about'.format(token=access_token.token) 

FWIW你可以pass the query part as a dict太(和它的实际的最佳实践):

url = 'https://graph.facebook.com/me' 
params = { 
    "access_token": access_token.token, 
    "fields": "id,name,email,posts,about" 
    } 
response = requests.get(url, params=params) 

requested_data = requests.get( 'SOME_URL')仅返回响应值

例如:

Ref图像:Response display

所以,如果你正在使用requests.get,那么你必须使用一些功能,从响应获取数据。

像requested_data.content这会导致JSON数据,您可以将其发送到模板并使用。

我用下面的示例代码来测试

import requests 
import json 

requested_data = requests.get('https://graph.facebook.com/me?access_token={my_fb_id_token}&fields=id,name,email') 
print("Requested Data Content= %s"%requested_data.content) 
print("Requested Data = %s"%requested_data) 

,并能够得到像下面

Requested Data Content= b'{"id":"1234569789011121","name":"Soma Naresh","email":"[email protected]"}' 
Requested Data = <Response [200]> 

让我知道,如果我错了的数据。