2
@error(404)
def error404(error):
    return 'Nothing here, sorry'

これは、 で 404 に応答する方法bottle frameworkです。しかし、404 では、 http://abc.com/などの特定の URL にリダイレクトしたいと考えています。出来ますか?

4

3 に答える 3

3
    @app.error(404)
    def error(err):
        bottle.response.status = 303 
        bottle.response.header['Location'] = '/' 
于 2011-06-21T07:53:00.970 に答える
3
@error(404)
def error404(error):
    from bottle import redirect
    # maybe test the error to see where you want to go next?
    redirect(new_url, 303) # HTTP 303 should be used in this case

編集これができるかどうかは100%確信が持てず、今はテストできませんが、後でテストして答えを更新します。

于 2011-01-16T16:22:26.780 に答える
-3
import urllib
url = urllib.urlopen('www.google.com/testing') #a 404 address
if url.code == 404:
    url = urllib.urlopen('www.google.com')

urlobject が作成されると、.code インスタンスはページのコードを返します。

于 2010-11-26T16:32:36.137 に答える