我終於有了一種方法來使用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>
我收到以下錯誤:
請讓我知道要改進什麼。
**在文本錯誤*
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
* i.stack.imgur.com目前無法處理此請求。*更之所以錯誤應該被添加作爲文本的一部分,而不是圖像。 –
@MosesKoledoye等一下我加入... –