tornado 整合jinja2,在应用到form欲上传数据时,会需要作如下的处理:
看解决方案:
<form action="/auth/login" method="post"> 用户名: <input type="text" name="username" placeholder="用户名"/> 密码: <input type="password" name="password" placeholder="密码"/> <input type="submit" value="登录" /> {{ xsrf_form_html()|safe}} </form>
class BaseHandler(tornado.web.RequestHandler): def get_current_user(self): username = self.get_secure_cookie("username") if not username: return None return username def get_context(self): self.context = { 'current_user':self.get_current_user(), } logging.info(self.xsrf_form_html) if config['app']['xsrf_cookies']: self.context['xsrf_form_html'] = self.xsrf_form_html return self.context
class AuthHandler(BaseHandler): def get(self): template = jinja_env.get_template('auth/login.html') self.write(template.render(self.get_context()))
这是我所想到的解决方案,希望能对大家有帮助 。