Abd*_*man 4 python django github-api python-requests
我正在使用 Python(3.6) 开发一个项目,我需要在其中实现 GitHub API。我尝试使用 JSON apis 作为:
来自views.py:
class GhNavigator(CreateView):
def get(self, request, *args, **kwargs):
term = request.GET.get('search_term')
username = 'arycloud'
token = 'API_TOKEN'
login = requests.get('https://api.github.com/search/repositories?q=' + term, auth=(username, token))
print(login)
return render(request, 'navigator/template.html', {'login': login})
Run Code Online (Sandbox Code Playgroud)
但它只是返回status 200,我想获取term用户传递的存储库列表。
我怎样才能做到这一点?
请帮帮我!
提前致谢!
The requests library will return a Response object if you perform a .get(..), .post(..) or anything like that. Since responses can be very huge (hundreds of lines), it does not print the content by default.
But the developers attached some convenient functions to it, for example to interpet the answer as a JSON object. A response object has a .json() function that aims to interpret the content as a JSON string, and returns its Vanilla Python counterpart.
So you can access the response (and render it the way you want), by calling .json(..) on it:
class GhNavigator(CreateView):
def get(self, request, *args, **kwargs):
term = request.GET.get('search_term')
username = 'arycloud'
token = 'API_TOKEN'
response = requests.get('https://api.github.com/search/repositories?q=' + term, auth=(username, token))
login = response.json() # obtain the payload as JSON object
print(login)
return render(request, 'navigator/template.html', {'login': login})Run Code Online (Sandbox Code Playgroud)
Now of course it is up to you to interpret that object according to your specific "business logic", and render a page that you think contains the required information.
| 归档时间: |
|
| 查看次数: |
673 次 |
| 最近记录: |