There is a better way for GET requests. There is a demo in the tornado source on github here
# url handler
handlers = [(r"/entry/([^/]+)", EntryHandler),]
class EntryHandler(BaseHandler):
def get(self, slug):
entry = self.db.get("SELECT * FROM entries WHERE slug = %s", slug)
if not entry: raise tornado.web.HTTPError(404)
self.render("entry.html", entry=entry)
Any "text" that matches the regular expression will be passed to the EntryHandler's get method as slug argument. If the url doesn't match any handler, the user will receive a 404 error.
If you wanted to provide another fallback, you could make the parameter optional
(r"/entry/([^/]*)", EntryHandler),
class EntryHandler(BaseHandler):
def get(self, slug=None):
pass
Update:
+1 for the link. However does this URL pattern extend to include more parameters if I wanted to search like this...
/recipes?ingredient=chicken&style=indian – colinjameswebb
Yes it does.
handlers = [
(r'/(d{4})/(d{2})/(d{2})/([a-zA-Z-0-9.:,_]+)/?', DetailHandler)
]
class DetailHandler(BaseHandler):
def get(self, year, month, day, slug):
pass
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…