Pyramid beaker accessing Sessions that have been created

2.9k views Asked by At

I am trying to use pyramid beaker in Pyramid framework and its just not working it creates the session objects but i cannot access them with the line

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

It gives the following error

KeyError

KeyError: 'beaker.session'

My development.ini file looks like this

# 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

and init.py like this

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
    )

I create the session like this

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

Where am i going wrong?

1

There are 1 answers

0
Blaise Laflamme On BEST ANSWER

You should be able to just use the include way and the pyramid_beaker package can initialize itself from the ini values.

in your ini file:

pyramid_includes = pyramid_beaker

or inside your main function's __init__.py file:

config.include('pyramid_beaker')

You can read more here http://docs.pylonsproject.org/projects/pyramid_beaker/en/latest/#setup

The usual way to access the session is through the request like you do in my_view:

session = request.session

The pyramid_beaker package use the pyramid session factory and the way it manages the session is not through the request.environement['beaker.session'] object like beaker's example. For more info read http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/sessions.html