将变量传递给具有依赖参数的另一个类

时间:2013-12-14 04:45:33

标签: python django box-api

我试图在Django中将access_token变量从一个类传递到另一个类。这是代码:

def select(request):

    code = request.REQUEST.get('code')
    response = finish_authenticate_v2(client_id, client_secret, code)
    access_token = str(response['access_token'])

    return render_to_response(context_instance=RequestContext(request))

def final(request):
    token = select(request)
    access_token = token.access_token

    return render_to_response(context_instance=RequestContext(request))

我收到错误:

Missing parameter. "code" is required

因为code中的select()是在select网页中从网址获取的,但code上没有final来抓取网址。有没有办法传递code以便我可以通过access_token

1 个答案:

答案 0 :(得分:1)

了解sessions are used in django的方式,django.auth module

也可以看到一个很好的用法示例
def select(request):
    ...
    request.session['access_token'] = str(response['access_token'])
    ...

def final(request):
    access_token = request.session['access_token']
    ...