问题
@error(404)
def error404(error):
return 'Nothing here, sorry'
This is the way to response 404 in bottle framework
. But On 404 I want to redirect to particular url say http://abc.com/. Is it possible?
回答1:
@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
EDIT I'm not 100% sure this can be done, and I can't test it right now, but I'll test it later and update the answer unless you beat me to it.
回答2:
@app.error(404)
def error(err):
bottle.response.status = 303
bottle.response.header['Location'] = '/'
回答3:
import urllib
url = urllib.urlopen('www.google.com/testing') #a 404 address
if url.code == 404:
url = urllib.urlopen('www.google.com')
When a urlobject is created the .code instance returns the code of the page,
来源:https://stackoverflow.com/questions/4286632/how-to-redirect-to-particular-url-on-404