自定义NotFound消息

问题

如何定义NotFound消息和其他消息?

解法

  1. import web
  2. urls = (...)
  3. app = web.application(urls, globals())
  4. def notfound():
  5. return web.notfound("Sorry, the page you were looking for was not found.")
  6. # You can use template result like below, either is ok:
  7. #return web.notfound(render.notfound())
  8. #return web.notfound(str(render.notfound()))
  9. app.notfound = notfound

要返回自定义的NotFound消息,这么做即可:

  1. class example:
  2. def GET(self):
  3. raise web.notfound()

也可以用同样的方法自定义500错误消息:

  1. def internalerror():
  2. return web.internalerror("Bad, bad server. No donut for you.")
  3. app.internalerror = internalerror