Django视图调用另一个视图而不使用请求

时间:2016-03-22 02:07:17

标签: django

我有一个观点,它会向http://127.0.0.1:80/o/token/发送请求 我想知道我可以直接调用视图/o/token/并获得结果吗?
并且不需要导入requests来发送它

class GetAccessToken(APIView):
    def post(self, request, *args, **kwargs):
        msg ={}
        return Response(msg, status=status.HTTP_200_OK)

    def get_access_token(self, username, password, client_id, client_secret, scope="read write"):
        url = "http://127.0.0.1:80/o/token/"
        payload = {
            'grant_type': 'password',
            'username': username,
            'password': password,
            'scope': scope
        }
        auth = HTTPBasicAuth(client_id, client_secret)
        try:
            response = requests.post(url, auth=auth, data=payload, verify=False, timeout=TIMEOUT)
        except Exception as err:
            print err

1 个答案:

答案 0 :(得分:0)

the source开始,您要调用的视图为TokenView。这意味着你应该能够做到:

from oauth2_provider.views import TokenView

# Call the view somewhere
TokenView.as_view()