I am using a form in my project having multiple pages to fill information, these below are some of the items in my left side menu to switch to different pages while filling the form. So currently I am using the functionality to redirect the page on the same tab when clicking on any of these items. But when I right click, open in new window or new tab window doesn’t appear. I want to enable open in a new tab only when right clicking on these items.
<li id="arrival_button" class="nav-item w-100 side_bar_buttons">
<a onclick="save_and_redirect('mainform','arrival_information')"
class="nav-link align-middle px-0 black_color">
<span class="ms-1 d-sm-inline">Arrival
Information</span>
</a
</li>
<li id="food_drinks_button" class="nav-item w-100 side_bar_buttons">
<a onclick="save_and_redirect('mainform', 'food_and_drinks');"
class="nav-link align-middle px-0 black_color">
<span class="ms-1 d-sm-inline">Food &
Drinks</span>
</a>
</li>
<li id="guest_button" class="nav-item w-100 side_bar_buttons">
<a onclick="save_and_redirect('mainform', 'guest_req');"
class="nav-link align-middle px-0 black_color">
<span class="ms-1 d-sm-inline">Guest Requirements</span>
</a>
</li>
I already know about using target="_blank" with tags in Django but obviously like I already explained I dont want to use this functionality to always open them in a new tab only when I want to right click on them.
2
Answers
Actually my Issue resolved after installing django-admin-contextmenu==0.1.0 in my project using pip install django-admin-contextmenu and after that updated it my settings file.
Added contextmenu to INSTALLED_APPS:
Now all tags where I was using href automatically updated the right click context menu in my whole website. And it this above sidebar I just added herfs and the pages links and now its working.
You can use JavaScript to handle the right-click event and open the link in a new tab programmatically.
oncontextmenu
attribute to each anchor tag (the links). By right-click on the link, theopenInNewTab
function will be called with the form and page as arguments. This function willopen a new window
with the specified URL.Make sure to replace
'your-url-here?form=' + form + '&page=' +
page with the actual URL you want to open in a new tab.And
return false;
is used to prevent the default context menu from showing up whenright-clicking
.Remember to adjust the URL in
window.open
according to the project’s structure and routing.I have updated the snippet. Please take a look.