我正在尝试复制并使用web.py cookbook中的“使用PostgreSQL数据库进行用户身份验证”示例。我无法弄清楚为什么会出现以下错误。
at / login 'ThreadedDict'对象没有属性'login'
at / login 'ThreadedDict'对象没有属性'privilege'
这是第二个错误输出到终端的错误。 (第一个几乎相同)
Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 239, in process return self.handle() File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 230, in handle return self._delegate(fn, self.fvars, args) File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 420, in _delegate return handle_class(cls) File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 396, in handle_class return tocall(*args) File "/home/erik/Dropbox/Python/Web.py/Code.py", line 44, in GET render = create_render(session.privilege) File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/session.py", line 71, in __getattr__ return getattr(self._data, name) AttributeError: 'ThreadedDict' object has no attribute 'privilege' 127.0.0.1:36420 - - [25/Aug/2012 01:12:38] "HTTP/1.1 GET /login" - 500 Internal Server Error
这是我的code.py文件。来自食谱的几乎切成糊状。我尝试将所有类和def放在主代码之上。我也试过用另一篇文章中提到的sudo启动python。
import web class index: def GET(self): todos = db.select('todo') return render.index(todos) class add: def POST(self): i = web.input() n = db.insert('todo', title=i.title) raise web.seeother('/') def logged(): return False #I added this to test error #1, Now I get error #2 #if session.login==1: # return True #else: # return False def create_render(privilege): if logged(): if privilege == 0: render = web.template.render('templates/reader') elif privilege == 1: render = web.template.render('templates/user') elif privilege == 2: render = web.template.render('templates/admin') else: render = web.template.render('templates/communs') else: render = web.template.render('templates/communs') return render class Login: def GET(self): if logged(): render = create_render(session.privilege) return '%s' % render.login_double() else: # This is where error #2 is render = create_render(session.privilege) return '%s' % render.login() def POST(self): name, passwd = web.input().name, web.input().passwd ident = db.select('users', where='name=$name', vars=locals())[0] try: if hashlib.sha1("sAlT754-"+passwd).hexdigest() == ident['pass']: session.login = 1 session.privilege = ident['privilege'] render = create_render(session.privilege) return render.login_ok() else: session.login = 0 session.privilege = 0 render = create_render(session.privilege) return render.login_error() except: session.login = 0 session.privilege = 0 render = create_render(session.privilege) return render.login_error() class Reset: def GET(self): session.login = 0 session.kill() render = create_render(session.privilege) return render.logout() #web.config.debug = False render = web.template.render('templates/', base='layout') urls = ( '/', 'index', '/add', 'add', '/login', 'Login', '/reset', 'Reset' ) app = web.application(urls, globals()) db = web.database(dbn='postgres', user='hdsfgsdfgsd', pw='dfgsdfgsdfg', db='postgres', host='fdfgdfgd.com') store = web.session.DiskStore('sessions') # Too me, it seems this is being ignored, at least the 'initializer' part session = web.session.Session(app, store, initializer={'login': 0, 'privilege': 0}) if __name__ == "__main__": app.run()
答案 0 :(得分:0)
好的,我能弄明白我做错了什么。总新手的东西和学习过程的所有部分。这段代码现在很有效。我坚持的那部分现在正在发挥作用。请参阅我在代码中的评论
由于
import web web.config.debug = False render = web.template.render('templates/', base='layout') urls = ( '/', 'index', '/add', 'add', '/login', 'Login', '/reset', 'Reset' ) app = web.application(urls, globals()) db = web.database(blah, blah, blah) store = web.session.DiskStore('sessions') session = web.session.Session(app, store, initializer={'login': 0, 'privilege': 0}) class index: def GET(self): todos = db.select('todo') return render.index(todos) class add: def POST(self): i = web.input() n = db.insert('todo', title=i.title) raise web.seeother('/') def logged(): if session.get('login', False): return True else: return False def create_render(privilege): if logged(): if privilege == 0: render = web.template.render('templates/reader') elif privilege == 1: render = web.template.render('templates/user') elif privilege == 2: render = web.template.render('templates/admin') else: render = web.template.render('templates/communs') else: ## This line is key, i do not have a communs folder, thus returning an unusable object #render = web.template.render('templates/communs') #Original code from example render = web.template.render('templates/', base='layout') return render class Login: def GET(self): if logged(): ## Using session.get('something') instead of session.something does not blow up when it does not exit render = create_render(session.get('privilege')) return '%s' % render.login_double() else: render = create_render(session.get('privilege')) return '%s' % render.login() def POST(self): name, passwd = web.input().name, web.input().passwd ident = db.select('users', where='name=$name', vars=locals())[0] try: if hashlib.sha1("sAlT754-"+passwd).hexdigest() == ident['pass']: session.login = 1 session.privilege = ident['privilege'] render = create_render(session.get('privilege')) return render.login_ok() else: session.login = 0 session.privilege = 0 render = create_render(session.get('privilege')) return render.login_error() except: session.login = 0 session.privilege = 0 render = create_render(session.get('privilege')) return render.login_error() class Reset: def GET(self): session.login = 0 session.kill() render = create_render(session.get('privilege')) return render.logout() if __name__ == "__main__": app.run()