web.py - WebPy Sessions with Templates -
using webpy. have following code in app.py:
app = web.application(urls, globals()) session = web.session.session(app, web.session.diskstore('sessions'), initializer={'count': 0, 'username': 'crc'}) render = web.template.render('templates/', base="base", globals={'context': session, 'username': 'crc'}) class index(object): def get(self): if not session: session.count = 0 else: session.count = 1 return render.hello_form() def post(self): form = web.input(name="nobody", greet="hello") greeting = "%s, %s" % (form.greet, form.name) return render.index(greeting = greeting) and following in index html file:
$def (greeting) <p> hi <b>$context.username</b> </p> $if greeting: wanted <em style="color: green; font-size: 2em;">$greeting</em>. $else: wanted <em style="color: green; font-size: 2em;">default</em>. <p> <a href="/hello">link form</a> i'm seeing once submit form: @ /hello 'threadeddict' object has no attribute 'username'
python /library/python/2.7/site-packages/web/session.py in getattr, line 64 web post /hello
any thoughts on why seeing this? doesn't doing username, it's not clear me why.
as error says, context variable (which mapped session), has no attribute username. instead, you've defined username global var itself.
try accessing username directly in template $username, or make assignment session['username'] = 'crc' in app.py, $context.username valid when access in template file.
Comments
Post a Comment