问题
I have been using web.py recently to try and create an web app, an issue became apparent that we did not have a way of creating multiple sessions, I have tried using beaker (https://github.com/bbangert/beaker) to manage the multiple sessions, but now I have run into an issue of rendering the home page, does anyone have an idea of how to fix this? the error we continue to get is
" BLAH BLAH BLAH BLAH Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/web/application.py", line 257, in process return self.handle() File "/usr/local/lib/python3.4/dist-packages/web/application.py", line 248, in handle return self._delegate(fn, self.fvars, args) File "/usr/local/lib/python3.4/dist-packages/web/application.py", line 488, in _delegate return handle_class(cls) File "/usr/local/lib/python3.4/dist-packages/web/application.py", line 466, in handle_class return tocall(*args) File "app.py", line 92, in GET return render.Home() AttributeError: 'NoneType' object has no attribute 'Home' "
import web
import datetime
from beaker.middleware import SessionMiddleware
from Models import RegisterModel, LoginModel, Posts
web.config.debug = False
urls = (
'/', 'Home',
"/register", "Register",
"/postreg", "PostRegistration",
'/login', 'Login',
'/check-login', "CheckLogin",
'/logout', 'Logout',
'/post-activity', 'PostActivity',
'/profile', 'Profile',
'/settings', 'Settings',
'/update-user', 'UpdateSettings',
'/admin', 'Admin'
)
t_globals = {
'datestr': web.datestr,
'str': str,
'datetime': datetime
}
app = web.application(urls, globals())
session = web.session.Session(app, web.session.DiskStore("sessions"),initializer={"user":None})
session_data = session._initializer
render = web.template.render("Views/Templates", base="MainLayout", globals={"session":session_data, "current_user": session_data["user"]})
# beaker options are optional.... but it's good to see
# where they are called
session_opts = {
'session.cookie_expires': True,
'session.timeout' : 600,
'session.secure' : True,
}
# define webPy app
# smash it all together and define name for cookie key (sid)
def session_mw(app):
return SessionMiddleware(app, config = session_opts, key = "sid")
# function to check if we are logged in
def logged(session):
if 'Login' in session:
if session['Login']==1:
return True
else:
return False
else:
return False
# create different renders based on log in privilege
def create_render(privilege):
session = web.ctx.environ['beaker.session']
if logged(session) == True:
if privilege == 0:
print("OH BABY")
render = web.template.render('/Views/Templates/user/', base='/Views/Templates/MainLayout', globals=t_globals)
elif privilege == 1:
print("LET ME LOVE YOU")
render = web.template.render('/Views/Templates/user/', base='/Views/Templates/MainLayout', globals=t_globals)
elif privilege == 2:
print("DONT HURT ME")
render = web.template.render('/Views/Templates/admin/', base='/Views/Templates/MainLayout', globals=t_globals)
else:
print("NOOT NOOT")
render = web.template.render('/Views/Templates/viewer', base='/Views/Templates/MainLayout', globals=t_globals)
render = web.template.render('/Views/Templates/', base='MainLayout', globals=t_globals)
return render
class Login:
def GET(self):
session = web.ctx.environ['beaker.session']
if logged(session) == True:
print("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH")
render = create_render(session['privilege'])
return render.Home()
else:
render = create_render(0)
print("BLAH BLAH BLAH BLAH")
return render.Home()
# I get user credentials from a database (called in model.get_users)
def POST(self):
session = web.ctx.environ['beaker.session']
data= web.input()
name = data['user']
password = data['password']
ident = model.get_users(m, name)
try:
if password == ident.password.strip():
session['Login'] = 1
session['privilege'] = ident['privilege']
session['user'] = name
session.save()
render = create_render(session['privilege'])
print("HERE")
return render.Home()
else:
session.login = 0
session.privilege = 0
session.user = 'none'
render = create_render(session['privilege'])
print("NO HERE YO")
return render.Login()
except:
session['Login'] = 0
session['privilege'] = 0
render = create_render(session['privilege'])
print("BANANA")
return render.Login()
# Credits go to bbangert for the middleware code
class Logout:
def GET(self):
session = web.ctx.environ['beaker.session']
try:
render = create_render(0)
session.invalidate()
session.delete()
print("TOPSY TURVY")
return render.Login()
except:
session.invalidate()
session.delete()
render = create_render(0)
print("MESSY RIGHT")
return render.Login()
class Home:
def GET(self):
session = web.ctx.environ['beaker.session']
if logged(session) == False:
raise web.seeother('/login')
else:
render = create_render(0)
return render.Home()
# '''
def GET(self):
data = type('obj', (object,), {'username':'q', 'password':'q'})
login = LoginModel.LoginModel()
isValid = login.check_user(data)
if isValid:
session_data['user'] = isValid
post_model = Posts.Posts()
posts = post_model.get_all_posts()
return render.Home(posts)
# '''
class Register:
def GET(self):
return render.Register()
class PostRegistration:
def POST(self):
print("DOING POST PROCESSING")
data = web.input()
reg_model = RegisterModel.RegisterModel()
reg_model.insert_user(data)
return data.username
class CheckLogin:
def POST(self):
print("DOING CHECK LOGIN")
data = web.input()
login = LoginModel.LoginModel()
isValid = login.check_user(data)
if isValid:
session_data["user"] = isValid
return isValid
return "error"
class PostActivity:
def POST(self):
print("DOING POST ACTIVITY")
data = web.input()
data.username = session_data['user']['username']
post_model = Posts.Posts()
post_model.insert_post(data)
return "success"
class Profile:
def GET(self):
return render.Profile()
class Settings:
def GET(self):
return render.Settings()
class UpdateSettings:
def POST(self):
data = web.input()
data.username = session_data['user']['username']
update = RegisterModel.RegisterModel()
update.update_settings(data)
return "success"
class Admin:
def GET(self):
return render.Admin()
if __name__ == "__main__":
app.run(session_mw)
来源:https://stackoverflow.com/questions/47294554/web-py-beaker-python-app-page-render-issues