webpy中的会话-在所有课程中获取用户名 [英] session in webpy - getting username in all classes

查看:85
本文介绍了webpy中的会话-在所有课程中获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目使用webpy框架.我正在从main.py程序的登录"类登录.我想在其他班级获得用户名.我尝试了会话并尝试了很长时间.我已经实现了如下所示的会话.

I am using webpy framework for my project. I am logging in from my 'login' class of main.py program. I want to get the username in some other class. I tried with session and experimented with it for a long time. I have implemented session like below.

store = web.session.DiskStore('sessions')
session = web.session.Session(app,store,initializer={'login': 0,'privilege': 0})

在我的main.py登录类中,以下代码将在用户提交用户名和密码时起作用(这是我的登录类的发布方法0f).

in my login class of main.py the following code will work when user submit username and password.(This is the post method 0f my login class).

class login:
    def POST(self):
            i = web.input(form_submit="Login")
            authdb = sqlite3.connect('database/users.db')
            conn = authdb.cursor()
            if i.form_submit=='Login':
                check = conn.execute('select * from user_details where username=?and password=?', (i.username, i.password))
                n=check.fetchall()
                if len(n)!=0:
                        session.loggedin = True
                        session.username = i.username
            return render.home('Home')
                else: return render.display('Wrong username or password !')

我想在其他班级获得用户名.我尝试使用session.username访问用户名,但显示以下错误. AttributeError: 'ThreadedDict' object has no attribute 'username'

I want to get the username in some other class. I tried to access the username with session.username , but it shows the following error. AttributeError: 'ThreadedDict' object has no attribute 'username'

推荐答案

只需在会话的initializer dict中提供loggedinusername的默认值.如果未设置这些属性(例如,当用户未登录时),并且您尝试访问它们,则似乎会出现错误.

Just provide the default values for loggedin and username in session's initializer dict. The error seems to appear when those attributes are not set (i.e. when user isn't logged in) and you try to access them.

这篇关于webpy中的会话-在所有课程中获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆