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
Viewclass has differentkwargsthan theget_context_datamethodThe
kwargsof aViewcontain the URL ParametersThe
kwargspassed to theget_context_datamethod are passed to the template for renderingSince you want to access a URL Parameter in
get_context_datayou would useself.kwargs(selfis referring to yourViewclass)