我正在使用self.render渲染html模板,这取决于通过ajax从客户端收到的信息,def post()如下所示:
class aHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
taskComp = json.loads(self.request.body)
if taskComp['type'] == 'edit':
if taskComp['taskType'] == 'task':
self.render(
"tasks.html",
user=self.current_user,
timestamp='',
projects='',
type='',
taskCount='',
resName='')
Run Code Online (Sandbox Code Playgroud)
但是,这不会将用户重定向到html页面'tasks.html'.
但是我在我的控制台中看到了一个状态:
[I 141215 16:00:55 web:1811] 200 GET /tasks (127.0.0.1)
Run Code Online (Sandbox Code Playgroud)
其中'/ tasks'是tasks.html的别名
为什么不重定向?
或者如何从ajax接收数据,然后用于重定向到tasks.html页面以及上述self.render请求中提供的所有参数?
"渲染"永远不会将访问者的浏览器重定向到其他URL.它向浏览器显示您呈现的页面内容,在本例中为"tasks.html"模板.
要重定向浏览器:
@tornado.web.authenticated
def post(self):
self.redirect('/tasks')
return
Run Code Online (Sandbox Code Playgroud)
更多信息在重定向文档中.
要使用AJAX响应重定向,请尝试将目标位置从Python发送到Javascript:
class aHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
self.write(json.dumps(dict(
location='/tasks',
user=self.current_user,
timestamp='',
projects='',
type='',
taskCount='',
resName='')))
Run Code Online (Sandbox Code Playgroud)
然后在Javascript中的AJAX响应处理程序中:
$.ajax({
url: "url",
}).done(function(data) {
var url = data.location + '?user=' + data.user + '×tamp=' + data.timestamp; // etc.
window.location.replace("http://stackoverflow.com");
});
Run Code Online (Sandbox Code Playgroud)
有关URL编码的更多信息就是这个答案.