how to redirect to a external 404 page python flask
Solution 1
You should try something like this:
from flask import render_template
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
Source http://flask.pocoo.org/docs/1.0/patterns/errorpages/
Solution 2
Try this instead of a route
from flask import request
@app.errorhandler(404)
def own_404_page(error):
pageName = request.args.get('url')
print(pageName)
print(error)
f = open('erreur404.tpl')
return f.read()
Solution 3
You cannot do this - the user-agent (in most cases, a browser) looks at the status code that is returned to determine what to do. When you return a 404 status code what you are saying to the user-agent is, "I don't know what this thing is you are requesting" and the user-agent can then:
- Display what you return in the body of the response
- Display its own error message to the end user
- Do some combination of the above two options
redirect
actually creates a little HTML response (via werkzeug.exceptions), which normally the end user doesn't see because the user-agent follows the Location
header when it sees the 302 response. However, you override the status code when you provide your own status code (404).
The fix is to either:
- Remove the status code (at the cost of sending the wrong signal to the end user, potentially)
-
or Send a 404 with a
meta:refresh
and / or JavaScript redirect (slightly better, still confusing):return redirect("/where-ever"), 404, {"Refresh": "1; url=/where-ever"}
Phosy
Updated on June 19, 2022Comments
-
Phosy almost 2 years
I am trying to redirect my 404 to a external URL like this:
@app.route('404') def http_error_handler(error): return flask.redirect("http://www.exemple.com/404"), 404
but it does not work. I keep getting:
Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
-
Phosy about 9 yearsI don't have the 404.html file on the same project I need to redirect to another website and still have my 404 status code I already look at the doc many times..
-
lapinkoira about 9 yearsWell you dont have to actually redirect to a template, do something like this return redirect("example.com", code=302)
-
lapinkoira about 9 yearsIt just an example to show how to handle the error, then inside you do whatever you want
-
lapinkoira about 9 yearsDude, just type 404, it's an example, what do u want, to just copy and paste?
-
lapinkoira about 9 yearsSo you are handing the 404 error but when should be redirected to exemple.com/404 you are getting this error instead? "Not Found The requested URL", like, some 500 internal server error?
-
Phosy about 9 yearsI'm getting this I already try this "Redirecting... You should be redirected automatically to target URL: exemple.com/404. If not click the link." but it never redirect ....
-
Jamie Lindsey over 5 yearsWhats actually hilarious is that now (2019), the link above return an actual 404 error page, hehe! the new link link is flask.pocoo.org/docs/1.0/patterns/errorpages
-
Jamie Lindsey over 5 yearsNearly every line in your example is very non-pythonic. You suggest to use CamelCase in place of snake_case, your reading a file template instead of return a response, and pointlessly printing the page name. Just my opinion.
-
Coder almost 5 yearswhat is request?
-
Quentin about 4 years@JohnD probably found out by now but that's referring to
flask.request