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.
Flask class has a redirect() function. When called, it returns a response object and redirects the user to another target location with specified status code. location parameter is the URL where response should be redirected. statuscode sent to browser's header, defaults to 302.
If you want your form to submit to a different route you can simply do <form action="{{ url_for('app. login') }}"> . If you just want to put a link to the page use the <a> tag. If you want to process the request and then redirect, just use the redirect function provided by flask.
To handle 404 Error or invalid route error in Flask is to define a error handler for handling the 404 error. @app. errorhandler(404) def invalid_route(e): return "Invalid route." Now if you save the changes and try to access a non existing route, it will return “Invalid route” message.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With