Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何重定向到404上的特定url_Python_Bottle - Fatal编程技术网

Python 如何重定向到404上的特定url

Python 如何重定向到404上的特定url,python,bottle,Python,Bottle,这是在瓶子框架中响应404的方法。但在404上,我想重定向到特定的url。可能吗 @error(404) def error404(error): return 'Nothing here, sorry' 创建URL对象时,.code实例返回页面的代码 import urllib url = urllib.urlopen('www.google.com/testing') #a 404 address if url.code == 404: url = urllib.urlop

这是在
瓶子框架
中响应404的方法。但在404上,我想重定向到特定的url。可能吗

@error(404)
def error404(error):
    return 'Nothing here, sorry'
创建URL对象时,.code实例返回页面的代码

import urllib
url = urllib.urlopen('www.google.com/testing') #a 404 address
if url.code == 404:
    url = urllib.urlopen('www.google.com')

编辑我不能100%确定这是否可以做到,我现在无法测试,但我会稍后测试并更新答案,除非你比我快。

你使用的瓶子是什么版本?我在瓶子里的错误处理方面遇到了麻烦——我想是因为文档中的代码不是最新的。我使用瓶子0.9.dev尝试了上面的重定向代码,得到了一个500响应代码,显示“严重错误”错误:HTTPResponse('HTTP response 303',)抱歉延迟,StackOverflow关闭了一段时间。该示例引用了Git的HEAD版本。它可能在这期间发生了变化,但我对此表示怀疑。您是否检查了应用程序中发生的确切错误?500通常表示你的代码中有错误。你能解释一下为什么它有效,而不仅仅是给出答案吗?
@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
    @app.error(404)
    def error(err):
        bottle.response.status = 303 
        bottle.response.header['Location'] = '/'