我跟着this Tutorial测试我的MySQL for Web.py Setup。
由于教程没有完整的源代码,我在这里发帖:
import web
import MySQLdb
web.config.debug = False
urls = (
'/login', 'Login',
'/reset', 'Reset',
)
app = web.application(urls, locals())
db = web.database(dbn='mysql', db='dbname', user='user', pw='password')
store = web.session.DiskStore('sessions')
session = web.session.Session(app, store,
initializer={'login': 0, 'privilege': 0})
def logged():
if session.login==1:
return True
else:
return False
def create_render(privilege):
if logged():
if privilege == 0:
render = web.template.render('/Users/me/Desktop/cpage/Templates/reader')
elif privilege == 1:
render = web.template.render('/Users/me/Desktop/cpage/Templates/user')
elif privilege == 2:
render = web.template.render('/Users/me/Desktop/cpage/Templates/admin')
else:
render = web.template.render('/Users/me/Desktop/cpage/Templates/communs')
else:
render = web.template.render('/Users/me/Desktop/cpage/Templates/login')
return render
class Login:
def GET(self):
if logged():
render = create_render(session.privilege)
return '%s' % render.login_double()
else:
render = create_render(session.privilege)
return '%s' % render.login()
def POST(self):
name, passwd = web.input().name, web.input().passwd
ident = db.select('example_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()
if __name__ == "__main__":
app.run()
桌面上的目录cpage包含如下:
DBTest.py模板login.html会话
在模板中还有另一个login.html,只是为了确保它存在,因为我遇到的问题是:服务器能够从终端启动(我正在运行MacOSX Mavericks) 但无论何时我输入localhost:port / login,它都会崩溃:
http://0.0.0.0:1234/
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/web/application.py", line 239, in process
return self.handle()
File "/Library/Python/2.7/site-packages/web/application.py", line 230, in handle
return self._delegate(fn, self.fvars, args)
File "/Library/Python/2.7/site-packages/web/application.py", line 420, in _delegate
return handle_class(cls)
File "/Library/Python/2.7/site-packages/web/application.py", line 396, in handle_class
return tocall(*args)
File "cpage/DBTest.py", line 48, in GET
return '%s' % render.login()
File "/Library/Python/2.7/site-packages/web/template.py", line 1017, in __getattr__
t = self._template(name)
File "/Library/Python/2.7/site-packages/web/template.py", line 1011, in _template
self._cache[name] = self._load_template(name)
File "/Library/Python/2.7/site-packages/web/template.py", line 1001, in _load_template
raise AttributeError, "No template named " + name
AttributeError: No template named login
答案 0 :(得分:0)
好吧,我自己想出来,可能是学习东西的最好方法。 无论哪种方式,我都想向世界提供答案。
首先,教程中的代码看起来有点不同,所以post helped me a lot.
使用此代码后(或更好:合并)我遇到了同样的问题。
我最后不得不重新组织我的文件夹结构。将layout.html添加到Templates文件夹可以解决问题。使用正确的目录路径也很重要。