Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to redirect to a external 404 page python flask

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.

like image 454
Phosy Avatar asked Apr 08 '15 13:04

Phosy


People also ask

How do I redirect to another page in Flask?

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.

How do I make a button action redirect to another page in Flask?

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.

How does a Flask handle 404?

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.


1 Answers

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/

like image 149
lapinkoira Avatar answered Sep 28 '22 03:09

lapinkoira