pythonhtmldjango-templatesdjango-urlsdynamic-url

request.path does not match expected dynamic url


I'm trying to highlight specific buttons in the navigation bar when I'm on a specific page and hyperlink to it when i'm not. The url for this page is a dynamic url however and using the {% url ... as var %} syntax is not making the specific buttons highlighted when they should be. The issue here is that the button does not even show up at all. I narrowed it down to the request.path not matching with the dynamic url path. Normal urls (without the /str:something) seem to match perfectly.

in navbar.html i've tried to define the comparison_url (before all other code) as follows, but nothing seems to work so far:

{% url 'comparison' as comp_url %}
{% url 'comparison' sub_name as comp_url %}
{% url 'comparison' suburb_name as comp_url %}
{% url 'comparison' sub_name=suburb_name as comp_url %}
{% url 'comparison' vergelijking.suburb as comp_url %}

urls are defined as follows in urls.py:

urlpatterns = [
    path("vergelijking/<str:sub_name>", views.ComparisonView.as_view(), name="comparison"),
    path("signalen/<str:sub_name>", views.RadarView.as_view(), name="signals"),
    path("oorzaken/<str:sub_name>", views.CausationsView.as_view(), name="causation"), 

]

in navbar.html, where the actual problem lies:

{% url 'comparison' sub_name as comp_url %}
{% url 'signals' sub_name as sig_url %}
{% url 'causation' sub_name as comp_url %}

{% if request.path == comp_url %}
     <li class="nav-item mx-0 mx-lg-1"><a class="nav-link py-3 px-0 px-lg-3 rounded active">Vergelijking</a></li>
{% elif request.path == sig_url or request.path == caus_url %}
    <li class="nav-item mx-0 mx-lg-1"><a class="nav-link py-3 px-0 px-lg-3 rounded" href="{% url 'comparison' sub_name=suburb_name%}">Vergelijking</a></li>
{% endif %}

my base.html includes the navbar.html and the comparison.html extends base.html. There is no view for the navbar.html or base.html. My comparisonview looks like this (municipality details are loaded into the context):

class ComparisonView(LoginRequiredMixin, TemplateView):
    """map of municipality and suburbs with tooltip info"""
    template_name = 'comparison.html'

def get_context_data(self, **kwargs):
    context = super(ComparisonView, self).get_context_data(**kwargs)
    context['suburb_name'] = self.kwargs['sub_name']
    municipality_name = Suburb.objects.get(name=self.kwargs['sub_name']).municipality.name
    context['municipality_name'] = municipality_name
    suburb_list = Suburb.objects.filter(municipality__name=municipality_name)
    suburbs_signals_dict = {}
    for s in suburb_list:
        suburbs_signals_dict[s.name] = s.get_signals()
    context['suburb_signals'] = dumps(suburbs_signals_dict)
    return context

I think the mistake is in the way i define my comp_url but i'm not exactly sure. Does anyone know what I'm doing wrong?


Solution

  • the view was missing a request as a parameter (whenever i was calling on request.path, it was equating the to be matched link with null as request.path did not exist). I solved this by adding the path to the context:

    context['path'] = quote(self.request.path)
    

    The quote was in my case necessary since self.request.path can have spaces in a url whereas comp_url has '%20' for spaces. After I could call on path like so:

    {% if path == comp_url %}
    

    And the problem was solved