如何在龙卷风路线中实现多个URL参数? [英] How can I implement multiple URL parameters in a Tornado route?
本文介绍了如何在龙卷风路线中实现多个URL参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图弄清楚如何用最多3个(可选)URL参数实现URL。
I'm trying to figure out how to implement a URL with up to 3 (optional) url parameters.
我想出了如何在ASP中做到这一点。 NET MVC 3,但由于当前项目的限制而将其淘汰。因此,这就是我要寻找的内容:
I figured out how to do this in ASP.NET MVC 3, but the constraints of the current project eliminated it. So, here's what I'm looking for:
base / {param1} / {param2} / {param3}
其中param2和param3是可选的。这只是处理程序
部分中的正则表达式模式吗?
base/{param1}/{param2}/{param3}
where param2 and param3 are optional. Is this simply a regex pattern in the handlers
section?
推荐答案
I '不确定是否有好方式,但这应该可行:
I'm not sure if there is a nice way to do it, but this should work:
import tornado.web
import tornado.httpserver
class TestParamsHandler(tornado.web.RequestHandler):
def get(self, param1, param2, param3):
param2 = param2 if param2 else 'default2'
param3 = param3 if param3 else 'default3'
self.write(
{
'param1': param1,
'param2': param2,
'param3': param3
}
)
# My initial answer is above, but I think the following is better.
class TestParamsHandler2(tornado.web.RequestHandler):
def get(self, **params):
self.write(params)
application = tornado.web.Application([
(r"/test1/(?P<param1>[^\/]+)/?(?P<param2>[^\/]+)?/?(?P<param3>[^\/]+)?", TestParamsHandler),
(r"/test2/(?P<param1>[^\/]+)/?(?P<param2>[^\/]+)?/?(?P<param3>[^\/]+)?", TestParamsHandler2)
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8080)
tornado.ioloop.IOLoop.instance().start()
这篇关于如何在龙卷风路线中实现多个URL参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文