Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:04:33+00:00 2026-05-29T07:04:33+00:00

I try to add a login functionality to the web.py todo example . This

  • 0

I try to add a login functionality to the web.py todo example.

This is my code:

""" 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()

When the user does a POST in /login, logged_in is always False.

Any ideas why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:04:33+00:00Added an answer on May 29, 2026 at 7:04 am

    I just fixed it. I was missing some session initialization code.
    Here’s the working code:

    """ 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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to add an addons system to my Windows.Net application using Reflection; but
I try to add a class to a td-element using javascript with the internet
If I add permission offline_access to my app, when I try to login it
I'm encountering this error : Membership credential verification failed. when I try to login
Please try this yourself :) ! curl http://www.windowsphone.com/en-US/apps?list=free the result is: <html><head><title>Object moved</title></head><body> <h2>Object
i try to code a login form which passes username and password to a
whenever i try to login or signup on my page i get this error:
I am trying to login to my web app using HttpWebRequest but I keep
I've added an onAuthenticationSuccessEvent to my config.groovy in order to try add a Login
i've been doing a login form by JSON encode, my code is like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.