10

Does anyone know if flask redirect is able to open a link / URL in a new tab?

@app.route('/test')
def my_page():

  return redirect('http://mylink.com', 301)

I would like if the mylink website opened in a new tab, any insights?

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
jmbmxer
  • 115
  • 1
  • 1
  • 6
  • I would think it would be easiest to have the link in the HTML open a new tab that points to `/test` and then let it redirect, like `New Tab Link`. If someone navigates directly to `/test` is there a reason you would want them to open a new tab? – Seberius Oct 01 '13 at 23:33
  • When the user visits /test and performs a POST request the python function actually creates the link and I would like the user to be sent to the link that is created and have it open in a new tab. I hope that makes sense. – jmbmxer Oct 02 '13 at 20:54

4 Answers4

15

As far as I know that would not be a flask issue. You have to open a new tab within your html code or with Javascript.

example: <a href="http://mylink.com" target="_blank">Link</a>

The server has no power over what the browser does in this case.

Community
  • 1
  • 1
m3o
  • 3,881
  • 3
  • 35
  • 56
4

You could also use Python's webbrowser module to open a page, if you'd like to avoid getting into the HTML

import webbrowser

@app.route('/test')
def my_page():
    return webbrowser.open_new_tab('http://mylink.com')
garettmd
  • 197
  • 2
  • 8
3

If you're dealing with a form you can set target="_blank"

0

A slight adjustment to @garettmd that fixes @John Jiang's issue:

import webbrowser
from flask import Flask, redirect, url_for

@app.route("/test/")
def test():
    webbrowser.open("https://google.com")
    return redirect(url_for("index"))

This way we get to return a redirect (which we should probably do anyway) and we also get our new tab without "The view function did not return a valid response" error.

  • This did not work for me. I got a new tab with google but my redirect still loaded in the previous page. – magM Oct 25 '21 at 09:21