passing an argument to ListView class and accessing using kwargs

608 views
Skip to first unread message

Patrick Carra

unread,
Dec 10, 2019, 9:33:08 PM12/10/19
to Django users
Hello I am attempting to pass an argument to a Class(ListView) and then perform a filter using a kwarg that contains the argument passed in the url.  I am having a problem because I cannot access the variable in the url using kwargs.  I am not sure what I'm doing wrong below is the link that I'm using to pass this value:
<a href="{% url 'viewUtilization' record.pathname %}" target="blank" class="edit-item" title="ViewUtilization">Click to View Utilization</a><br>

my viewLit/urls.py is:
urlpatterns= [
     path('<path:pk>/', views.viewLit, name='viewLit'),
     path('<path:circuitid>/edit', views.editLit.as_view(), name='editLit'),
     path('<path:circuitid>/editXC', views.editXC.as_view(), name='editXC'),
     path('<path:pathname>/viewutilization', views.viewUtilization.as_view(), name='viewUtilization'),
]

my views.py is:
class viewUtilization(ListView):
    pk_url_kwarg = 'pathname'
    model=Utilizationtable

    def get_queryset(self, **kwargs):
        qs = Utilizationtable.objects.filter(pathname=kwargs)
        return qs


and my template is:
<html>
<style>
</style>
<body>
You have made it to the utilization page
{% block content %}
  <h2>Utilization</h2>
    {% for object in object_list %}
      {{ object.reportdate }} {{ object.utilization }}
    {% endfor %}
{% endblock %}
</body>
</html>

My query comes back as this:
SELECT ••• FROM "utilizationtable" WHERE "utilizationtable"."pathname" = '{}'

I know I'm missing something simple but I'm not sure what?!?!?!?!??!!

Patrick Carra

unread,
Dec 10, 2019, 9:49:02 PM12/10/19
to Django users
One more additional note this is the url that gets access when the link is clicked

Dominick Del Ponte

unread,
Dec 11, 2019, 1:12:17 AM12/11/19
to django...@googlegroups.com
I think you need to add ** to kwargs.
like: qs = Utilizationtable.objects.filter(pathname=**kwargs)

alternatively, this may also work,
def get_queryset(self):
        return self.model.objects.filter(pathname=self.kwargs['pathname'])



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/663edf79-246c-47d5-abb5-d9cb0e6ae718%40googlegroups.com.

Patrick Carra

unread,
Dec 11, 2019, 2:15:29 PM12/11/19
to Django users
Thank you Dominick your second suggestion worked beautifully!  I appreciate your time and expertise!
Reply all
Reply to author
Forward
0 new messages