Reputation: 1382
I am making a webapp in python using web.py, I have set up the tables and can login the user and everything, but the initializer for sessions doesn't seem to work.
I have the following in my code:
store = web.session.DBStore(db, 'sessions')
session = web.session.Session(app, store, initializer={'logged_in': 0, 'username': ''})
render = web.template.render('templates/', base='base', globals={'session': session, 'username': session.username})
But this throws the error: AttributeError: 'ThreadedDict' object has no attribute 'username'
What can be done? I basically just followed the example here:
http://webpy.org/cookbook/sessions
Upvotes: 1
Views: 4233
Reputation: 354
I had the same issue, so i used the _initializer property to get session data.
Here's an example:
store = web.session.DBStore(db, 'sessions')
session = web.session.Session(app, store, initializer={'logged_in': 0, 'username': ''})
session_data = session._initializer
render = web.template.render('templates/', base='base', globals={'session': session_data, 'username': session_data['username']})
You can then access all the data you declared in the initializer from session. If anyone has a better way, please let me know.
PS: I know its late, but better late then never...
Upvotes: 3
Reputation: 1188
You are trying to get a session variable before it's set.
I've noticed that session.username
works in some circumstances. But under WSGI it fails.
This works fine and is in the webpy docs :
session.get('username', False)
Not :
session.username
If you want to use the presets in the initializer, use the method in @Antonis-kalou 's answer.
session_data = session._initializer
session.get('username', session_data['username'])
Upvotes: 1
Reputation: 4479
Session is only loaded when request is processed, you cannot get its attributes during setup.
Upvotes: 2