Tornado next query string URL parameter

老子叫甜甜 提交于 2019-12-24 09:25:19

问题


Question

Since Tornado append a next query string parameter, which contain the URL of the resource building the redirect URL, sometimes we can redirect the user back to the referring page after login or logout applying line self.redirect(self.get_argument("next", "/"))

But when I use this in the following code, It didn't work. Without login, when visiting Page /test, it will redirect to /login?next=%2Ftest, but the parameter next is always null and it will be redirect to root page instead of /test.

How can I fix this problem?

code

import os
import tornado.wsgi
import tornado.web
import tornado.options
import tornado.ioloop

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self):
        return self.get_secure_cookie("user")

class MainHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        self.render('index.html', user = self.current_user)

class LoginHandler(BaseHandler):
    def get(self):
        if self.get_current_user():
            self.redirect('/')
            return
        self.render('login.html')

    def post(self):
        self.set_secure_cookie("user", self.get_argument("username"))
        self.redirect(self.get_argument('next', '/'))

class LogoutHandler(BaseHandler):
    def get(self):
        if not self.get_current_user():
            self.redirect('/')
            return
        self.clear_cookie("user")
        self.redirect(self.get_argument("next", "/"))

class TestHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        self.write("Testing!)

settings = { 
    "static_path" : os.path.join(os.path.dirname(__file__), "static"),
    "template_path" : os.path.join(os.path.dirname(__file__), "templates"),
    "login_url" : "/login",
    "cookie_secret" : "61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
    "gzip" : True,
    "debug" : True,
}

application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/login", LoginHandler),
    (r"/logout", LogoutHandler),
    (r"/test", TestHandler),
], **settings)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

回答1:


In the get method of LoginHandler, use the next argument for the redirect

...

class LoginHandler(BaseHandler):
    def get(self):
        if self.get_current_user():
            self.redirect(self.get_argument('next', '/')) # Change this line
            return
        self.render('login.html')

...


来源:https://stackoverflow.com/questions/20290020/tornado-next-query-string-url-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!