客户端按下按钮(method =“post”)后,我会在数据库中执行某些操作,然后刷新客户端的当前页面。我该怎么做?
答案 0 :(得分:4)
项目树:
$ tree
.
├── static
│ └── scripts
│ └── test.js
├── templates
│ └── index.html
└── test.py
龙卷风代码:
# test.py
import os
import logging
import time
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.gen
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class IndexHandler(tornado.web.RequestHandler):
def initialize(self, data):
self.data = data
def get(self):
self.render("index.html", data=self.data["status"])
@tornado.web.asynchronous
@tornado.gen.engine
def post(self):
action = self.get_argument('action')
if action == 'reload':
logging.info('background job start')
yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 3)
logging.info('background job stop')
self.data["status"] = "reloaded"
self.finish("{}")
class Application(tornado.web.Application):
def __init__(self):
self.data = {"status":"initialized"}
handlers = [
(r"/", IndexHandler, {"data":self.data}),
]
settings = dict(
static_path = 'static',
template_path=os.path.join(os.path.dirname(__file__), "templates"),
)
tornado.web.Application.__init__(self, handlers, **settings)
if __name__ == "__main__":
tornado.options.parse_command_line()
app = Application()
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
模板页面:
<!-- index.html -->
<html>
<head>
<title>JavaScript reload example</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="{{ static_url('scripts/test.js') }}" type="application/javascript"></script>
</head>
<body>
<input type="submit" value="reload" id="reload-button" /><br/>
{{data}}
</body>
</html>
JavaScript文件:
/* test.js */
$(document).ready(function() {
$('#reload-button').click(function(event) {
jQuery.ajax({
url: '//localhost:8888/',
type: 'POST',
dataType: 'json',
data: {
action: 'reload',
},
success: function() {
window.location.reload(true);
}
});
});
});
我正在让add_timeout
模拟数据库操作。