How to pass a parameter to Class based views in Django?

how to pass this parameter in the class called?

This is url

path('all-agency/<int:id>', AllAgenciesView.as_view(), name='all-agency'),


class AllAgenciesView( TemplateView):

class AllAgenciesListView(ServerSideDatatableView):
    def get_queryset(self):
      agencies = SettingsAgency.objects.using('agency-namespace').filter(id=self.kwargs['id'])
      return  agencies

this is in template

#in anchor tag(html)
<a href="{% url 'all-agency' id=2 %}" ><div>Agencies</div></a>

in address bar it would look like this:

Now I want to pass the 'id' into the class based views but when i did filter(id=self.kwargs['id'). I Got returned KeyError: 'id'

i tried another way which is


and it returned None when i print(agencies)

another way ive tried


this also returned the same None

You're calling the wrong view in your paths, you should call AllAgenciesListView instead of AllAgenciesView.

Corrected version:

path('all-agency/<int:id>', AllAgenciesListView.as_view(), name='all-agency'),
Back to Top