Re: [tornado] Login page with Tornado

830 views
Skip to first unread message

Roey Berman

unread,
Jan 11, 2013, 9:03:05 AM1/11/13
to python-...@googlegroups.com

The problem is you have the form's action set to / instead of /login/

On Jan 11, 2013 3:57 PM, "Amicns" <ansuman...@gmail.com> wrote:
Hi 

I searched for how to create a login page in tornado and I found following in the stackoverflow. 


I tried to execute the code by I am not able to set the route properly. 

I am using following for the login.html

<html>
<head>
</head>
<body>
<form method="post" action="/" name="aform" target="_top">
Login:<input type="text" name="login">
Password:<input type="password" name="password">
<input type="submit" value="Enter">
</form>
</body>
</html>

I am using following for the tornado server file

import os
import base64
import uuid
import tornado.web
import tornado.httpserver
import tornado.ioloop


ROOT = os.path.dirname(os.path.abspath(__file__)) + os.sep
        
class BaseHandler(tornado.web.RequestHandler):

    def get_login_url(self):
        return "/login"

    def get_current_user(self):
        user_json = self.get_secure_cookie("user")
        if user_json:
            return tornado.escape.json_decode(user_json)
        else:
            return None
        
class LoginHandler(BaseHandler):

    def get(self):
        self.render("login.html", next=self.get_argument("next","/"))

    def post(self):
        username = self.get_argument("login", "")
        password = self.get_argument("password", "")
        print username
        print password
        if True:
            self.redirect(self.get_argument("next", u"/"))
        else:
            self.redirect("/login")

    def set_current_user(self, user):
        if user:
            self.set_secure_cookie("user", tornado.escape.json_encode(user))
        else:
            self.clear_cookie("user")

class LogoutHandler(BaseHandler):

    def get(self):
        self.clear_cookie("user")
        self.redirect(u"/login")

class Index(BaseHandler):

    def get(self):
        self.write("hello world")

if __name__ == "__main__":

    app = tornado.web.Application(
            handlers=[
                (r'/', Index),
                (r'/login/', LoginHandler),
                (r'/logout/', LogoutHandler),
                ],
            cookie_secret = base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes),
            template_path = ROOT + 'template',
            static_path = ROOT + 'static'
            )    
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
        

Thanks for any help.

Reply all
Reply to author
Forward
0 new messages