问题

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

解法

import web

urls = (...)
app =  web.application(urls, globals())

def notfound():
    return web.notfound("Sorry, the page you were looking for was not found.")

    # You can use template result like below, either is ok:
    #return web.notfound(render.notfound())
    #return web.notfound(str(render.notfound()))

app.notfound = notfound

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

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

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

def internalerror():
    return web.internalerror("Bad, bad server. No donut for you.")

app.internalerror = internalerror

由于博客空间在国外,所以有时会造成不稳定(你懂滴),如果您觉得我的博文对您有帮助, 建议大家多使用RSS访问阅读,本站所有文章均已全文输出。
鲜果阅读器订阅图标

Fork me on GitHub