登录Web.py待办事项列表

时间:2012-02-03 19:44:01

标签: python session web.py

我尝试向web.py todo example添加登录功能。

这是我的代码:

""" Basic todo list using webpy 0.3 """
import web
import model

### Url mappings

urls = (
    '/', 'Index',
    '/login', 'Login',
    '/logout', 'Logout',
    '/del/(\d+)', 'Delete',
)


### Templates
render = web.template.render('templates', base='base')
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore('sessions'))

allowed = (
    ('user','pass'),
    ('tom','pass2')
)

class Login:

    login_form = web.form.Form( web.form.Textbox('username'),
        web.form.Password('password'),
        web.form.Button('Login'),
        )

    def GET(self):
        f = self.login_form()
        return render.login(f)

    def POST(self):
        # Validation
        if not self.login_form.validates():
            print "it didn't validate!"

        session.logged_in = True
        raise web.seeother('/')


class Logout:
    def GET(self):
        session.logged_in = False
        raise web.seeother('/')

class Index:

    form = web.form.Form(
        web.form.Textbox('title', web.form.notnull, 
            description="I need to:"),
        web.form.Button('Add todo'),
    )

    def GET(self):
        print "logged_in " + str(session.get('logged_in', False))
        if session.get('logged_in', False):
            """ Show page """
            todos = model.get_todos()
            form = self.form()
            return render.index(todos, form)
        else:
            raise web.seeother('/login')

    def POST(self):
        """ Add new entry """
        form = self.form()
        if not form.validates():
            todos = model.get_todos()
            return render.index(todos, form)
        model.new_todo(form.d.title)
        raise web.seeother('/')



class Delete:

    def POST(self, id):
        """ Delete based on ID """
        id = int(id)
        model.del_todo(id)
        raise web.seeother('/')


app = web.application(urls, globals())

if __name__ == '__main__':
    app.run()

当用户在POST/login时,logged_in始终为False

任何想法为什么?

1 个答案:

答案 0 :(得分:1)

我刚修好了。我错过了一些会话初始化代码。 这是工作代码:

""" Basic todo list using webpy 0.3 """
import web
import model

### Url mappings

urls = (
    '/', 'Index',
    '/login', 'Login',
    '/logout', 'Logout',
    '/del/(\d+)', 'Delete',
)


web.config.debug = False
render = web.template.render('templates', base='base')
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore('sessions'))

allowed = (
    ('user','pass'),
)

class Login:

    login_form = web.form.Form( web.form.Textbox('username', web.form.notnull),
        web.form.Password('password', web.form.notnull),
        web.form.Button('Login'),
        )

    def GET(self):
        f = self.login_form()
        return render.login(f)

    def POST(self):
        if not self.login_form.validates():
            return render.login(self.login_form)

        username = self.login_form['username'].value
        password = self.login_form['password'].value
        if (username,password) in allowed:
            session.logged_in = True
            raise web.seeother('/')

        return render.login(self.login_form)


class Logout:
    def GET(self):
        session.logged_in = False
        raise web.seeother('/')

class Index:

    form = web.form.Form(
        web.form.Textbox('title', web.form.notnull, 
            description="I need to:"),
        web.form.Button('Add todo'),
    )

    def GET(self):
        if session.get('logged_in', False):
            """ Show page """
            todos = model.get_todos()
            form = self.form()
            return render.index(todos, form)
        else:
            raise web.seeother('/login')

    def POST(self):
        """ Add new entry """
        form = self.form()
        if not form.validates():
            todos = model.get_todos()
            return render.index(todos, form)
        model.new_todo(form.d.title)
        raise web.seeother('/')



class Delete:

    def POST(self, id):
        """ Delete based on ID """
        id = int(id)
        model.del_todo(id)
        raise web.seeother('/')


app = web.application(urls, globals())

if web.config.get('_session') is None:
    session = web.session.Session(app, web.session.DiskStore('sessions'), {'count': 0})
    web.config._session = session
else:
    session = web.config._session

if __name__ == '__main__':
    app.run()