2012-01-17 74 views
4

我試圖用金字塔燒杯在金字塔框架,它只是沒有工作,那麼創建會話對象,但我不能用線金字塔燒杯訪問會話已創建

@view_config(route_name='load_qli', renderer='json') 
def load_qli(request): 
    request.environ['beaker.session'] 

訪問它們它提供了以下錯誤

KeyError 

KeyError: 'beaker.session' 

我development.ini文件看起來像這樣

# pyramid_beaker settings 
session.type = file 
session.data_dir = %(here)s/data/sessions/data 
session.lock_dir = %(here)s/data/sessions/lock 
session.key = customerskey 
session.secret = customerssecret 
session.cookie_on_exception = true 

和init.py這樣

from pyramid.config import Configurator 
from sqlalchemy import engine_from_config 
from qlipe.models import DBSession 
from pyramid_mailer import mailer_factory_from_settings 
from pyramid_beaker import session_factory_from_settings 


def main(global_config, **settings): 
    """ This function returns a Pyramid WSGI application. 
    """ 
    engine = engine_from_config(settings, 'sqlalchemy.') 
    DBSession.configure(bind=engine) 


    # pyramid_beaker add-on 
    session_factory = session_factory_from_settings(settings) 

    config = Configurator(
     settings=settings, 
     session_factory=session_factory 
    ) 

我創建這樣

def my_view(request): 
    session = request.session 
    session['name'] = 'Fred Smith' 
    session.save() 

會話我在哪裏去了?

+0

我不熟悉金字塔;你不能只在第一個代碼示例中執行'request.session'?傳遞給'my_view'的請求和傳遞給'load_qli'的請求有什麼不同? – AdamKG

回答

5

你應該只能使用include方法,pyramid_beaker包可以從ini值初始化自己。

pyramid_includes = pyramid_beaker 

或你的主函數的__init__.py文件中:

config.include('pyramid_beaker') 

你可以在這裏閱讀更多http://docs.pylonsproject.org/projects/pyramid_beaker/en/latest/#setup

的常用方法訪問會話

在ini文件

是通過您在my_view中的請求來完成的:

session = request.session 

pyramid_beaker包使用金字塔會話工廠,並且它管理會話的方式不是通過像燒杯示例那樣的request.environement ['beaker.session']對象。欲瞭解更多信息,請參閱http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/sessions.html