-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to share session in the child application? #141
Comments
Parent: |
Oh, I have written "session = web.session.Session(app, store, initializer={'login': 0, 'username': ''})" in my parent application. After I modified like you and used 'session.login' in the child application, it reported an error as "AttributeError: 'NoneType' object has no attribute 'login'". So it just means the session I get is None. |
Let's use assertions to see where the problem lies... Parent: web.cxt.session = web.session.Session(app, store, initializer={'login': 0, 'username': ''})
assert web.cxt.session, "Login failed!" Child: session = dict(web.ctx).get('session')
assert session, "Invalid session provided." |
Traceback (most recent call last): Ok, Invaid session provided |
Are we sure that the parent lines have already been run? |
# parent.py
import web
import child
web.config.debug = False
urls = (
'/child', child.app_child,
)
app = web.application(urls, globals())
db = web.database(
dbn='mysql',
host='127.0.0.1',
port=3306,
user='root',
pw='password',
db='webpy',
)
store = web.session.DBStore(db, 'sessions')
session = web.session.Session(app, store, initializer={'login': 0, 'username': ''})
web.ctx.session = session
if __name__ == '__main__':
app.run() # child.py
import web
web.config.debug = False
urls = (
'/hello', 'hello'
)
app_child = web.application(urls, globals())
session = dict(web.ctx).get('session')
class hello():
def GET(self):
return session.login Something like that! |
Nope. (The previous post was reformatted with ```python and ``` for readability.) The problem is that the line |
So that's it, how can I run after the parent.py? |
Many ways to do it but perhaps the simplest is... import web
web.config.debug = False
urls = (
'/hello', 'hello'
)
app_child = web.application(urls, globals())
class hello():
def GET(self):
return web.ctx.session.login |
AttributeError: 'ThreadedDict' object has no attribute 'session' No way, I think I just return to initial time! The simplest way you gave is the same as official cookbook.But it's version 0.3,and I use version 0.61.And I have tried the official cookbook twelve hours ago. I need the latest usage! |
I use python38, not python2 |
The version of official cookbook is still 0.3 but I use the latest 0.61.When I learn how to share session in the child application, the solution given by official cookbook can't work. For example, after I used "session = web.ctx.session" in the child application, it reported an error as "AttributeError: 'ThreadedDict' object has no attribute 'session'" .It just did't share the session at all.
What should I do?
The text was updated successfully, but these errors were encountered: