pacdev
pacdev

Reputation: 581

Is there a way in Django to mask link in navbar according to the loaded template?

I would like to do a simple thing in Django but cannot find the "Django" way to do it and I am sure that there is one.

Let's imagine I have a simple navbar as follow:

<ul>
    <li>
        <a href="{% url 'home-index' %}">Home</a>
    </li>
    <li>
        <a href="{% url 'blog-index' %}">Blog</a>
    </li>
</ul>

When I am at the 'blog-index' url, I want to hide this specific link in the navbar.

Upvotes: 0

Views: 251

Answers (2)

Mahammadhusain kadiwala
Mahammadhusain kadiwala

Reputation: 3635

===== navbar code =====

<nav class="navbar navbar-expand-lg bg-dark navbar-dark">
    <div class="container-fluid">
      <a class="navbar-brand" href="#">My Site</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" {% if request.resolver_match.url_name == 'home' %}style="display: none;"{% endif %} href="{% url 'home' %}">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" {% if request.resolver_match.url_name == 'service' %}style="display: none;"{% endif %} href="{% url 'service' %}">Service</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" {% if request.resolver_match.url_name == 'contactus' %}style="display: none;"{% endif %} href="{% url 'contactus' %}">Contact Us</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" {% if request.resolver_match.url_name == 'aboutus' %}style="display: none;"{% endif %} href="{% url 'aboutus' %}">About Us</a>
          </li>
        </ul>

       
      </div>
    </div>
  </nav>

==== urls.py ====

from django.urls import path
from .views import *
 
urlpatterns = [
    
    path('', HomeView,name='home'),
    path('service/', ServiceView,name='service'),
    path('aboutus/', AboutUsView ,name='aboutus'),
    path('contactus/', ContactUsView,name='contactus'),
   
]

===== Output =====

enter image description here

Upvotes: 1

nigel239
nigel239

Reputation: 1765

You can check the request.path in the template.

{% if request.path == "your_url" %}
    (dont show button)
{% endif %}

You can use the if statement to set the style to hidden for example.

If you want to use dynamic URL's, you could use a template tag:

# Put your navbar items in another HTML template.
@register.inclusion_tag('navbar.html', takes_context=True)
def navbar_tag(context):
    request = context['request']
    url_one = reverse('blog:your-index')
    url_two = reverse('blog:blog') # You get the gist, this could possibly be done automatically.
    return {
        "urls":{
            "url_one":url_one,
            "url_two":url_two,
        }
    }

And then in your template tag HTML template, you could use:

{% if request.path != urls.url_one %}

And in your normal template, you load the tag first, and can then just put it in place of your navbar items, like so:

{% load your_tag_library %}

{% navbar_tag %}

Template tag docs

Upvotes: 2

Related Questions