I want to add a parameter somevar
to my listview:
{% url 'products' somevar %}?"
in the urls:
path("products/<int:somevar>", ProductListView.as_view(), name = 'products'),
in the views:
class ProductListView(ListView):
def get_template_names(self):
print(self.kwargs) # {"somevar": xxxxx}
return f"{TEMPLATE_ROOT}/products.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
self.get_products().order_by("number")
print(kwargs) # kwargs is {} here
return context
def get_queryset(self):
return super().get_queryset().order_by("number")
How can I pass the variable to the get_context_data
method? I tried defining a get
method and call self.get_context_data(**kwargs)
within, but this leads to an object has no attribute 'object_list'
error.
The View
class has different kwargs
than the get_context_data
method
The kwargs
of a View
contain the URL Parameters
The kwargs
passed to the get_context_data
method are passed to the template for rendering
Since you want to access a URL Parameter in get_context_data
you would use self.kwargs
(self
is referring to your View
class)