2016-11-24 6 views
1

Python 2.7とDjango 1.10の2.12.1ライブラリを使用して、Webサービスを使用しています。セッションを使用してクッキーを保存し、永続性を使用し、Webサービスを使用せずに10秒間を渡すと、私のビューはオブジェクト要求を再生成します。セッション...(0123)私の見解がクッキーを変えたからです。なぜ私のリクエストセッションは有効期限ですか?

これは私のViews.pyです:

client_session = requests.Session() 

@watch_login 
def loginUI(request): 
     response  = client_session.post(URL_API+'login/', data={'username': username, 'password': password,}) 
     json_login  = response.json() 

@login_required(login_url="/login/") 
def home(request): 
    response_statistics = client_session.get(URL_API+'statistics/') 
    log('ClientSession: '+str(client_session)) 

    try: 
     json_statistics  = response_statistics.json() 
    except ValueError: 
     log('ExcepcionClientSession: '+str(client_session)) 

     return logoutUI(request) 

    return render(request, "UI/home.html", { 
     'phone_calls'   : json_statistics['phone_calls'], 
     'mobile_calls'   : json_statistics['mobile_calls'], 
     'other_calls'   : json_statistics['other_calls'], 
     'top_called_phones'  : json_statistics['top_called_phones'], 
     'call_ranges_week'  : json_statistics['call_ranges_week'], 
     'call_ranges_weekend' : json_statistics['call_ranges_weekend'], 
     'access_data'   : accessData(request.user.username), 
    }) 

    def userFeaturesFormInit(clientRequest): 
    log('FeaturesClientSession: '+str(client_session)) 
    response = client_session.get(URL_API+'features/') 

    try: 
     json_features = response.json() 
    except ValueError as e: 
     log('ExcepcionFeaturesClientSession: '+str(client_session)) 
     raise e 

ありがとうございました。

+0

あなたはジャンゴを使用していますか? – Marcs

+0

はい、私はdjangoを使用しています。 – ILoveYouDrZaius

答えて

0

クッキーを手動で指定し、リクエストに保存するように修正しました。

client_session = requests.Session() 
response = client_session.post(URL_API+'login/', {'username': username, 'password': password,}) 
request.session['cookiecsrf']  = client_session.cookies['csrftoken'] 
request.session['cookiesession'] = client_session.cookies['sessionid'] 

となります/ポストでそれを送信する:

cookies = {'csrftoken' : request.session['cookiecsrf'], 'sessionid': request.session['cookiesession']} 
response = requests.get(URL, cookies=cookies)