flask redirect to url

To redirect to a URL using Flask, you can use the redirect() function from the flask module. Here are the steps to achieve this:

  1. Import the redirect function from the flask module: from flask import redirect

  2. In your Flask route function, call the redirect() function and pass the URL you want to redirect to as an argument: @app.route('/redirect') def redirect_example(): return redirect('https://www.example.com')

  3. The redirect() function will create a new response object with a 302 status code and the provided URL as the Location header. This response object will redirect the user to the specified URL.

That's it! When a user visits the /redirect route in your Flask application, they will be automatically redirected to the specified URL.