How to open url in a new tab in Django?
Asked Answered
H

4

14

I have to open the result page using render_to_response on a new tab.

Hipparchus answered 24/3, 2011 at 6:37 Comment(0)
M
21

Django is server-side, opening in a new tab is client-side. So use an <A> with a target="_blank"

http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=target%3Dblank

But of course spawning new windows/tabs is annoying for the user, so try not to do that after all.

Mandorla answered 24/3, 2011 at 6:46 Comment(6)
thanks , but my control is in views.py i want the control to transferred to a new page and it must open in new tab.Hipparchus
It has to be done in the template that gets written out, in the HTML.Mandorla
Not exactly: you could code a middleware that will change your html, search for A tags and add the target="_blank".Behrens
Thanks for making the server/client side distinction. It all makes sense nowKingfish
Also, don't forget to use noopener like this: <a href="http://example" target="_blank" rel="noopener">, as it's a security vulnerability if you forget.Shlomo
more about "noopener"Crocodile
M
9

Most the time, loading the page in a new tab can be a real pain in the ar** for the user. Nevertheless it can still be necessary sometimes. If you really need to render your POST results in a new tab, use the target="_blank" as an attribute of your <form>.

Merciful answered 14/4, 2015 at 15:16 Comment(2)
Also, don't forget to use noopener like this: <a href="http://example" target="_blank" rel="noopener">, as it's a security vulnerability if you forget.Shlomo
more about "noopener"Crocodile
T
0

this was problem when i was passing the dynamic URL in DTL. i also solved by putting the target="_blank" after the href closed.

      <h3 class="card-title">
                        <i class="fas fa-bell fa-2x mr-1"></i>
                        <a href="{% url 'alerts' site=data.site.name  %}  " target ="_blank">Notifications </i></a>
                     </h3>
Tann answered 21/11, 2020 at 16:6 Comment(0)
F
0

<a></a> with target="_blank" below can open url in a new tab:

<a href="https://www.google.com/" target="_blank">Google</a>

And, <a></a> without target="_blank" below can open url in the current tab:

<a href="https://www.google.com/">Google</a>
Furgeson answered 31/7, 2023 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.