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 differentkwargs
than theget_context_data
methodThe
kwargs
of aView
contain the URL ParametersThe
kwargs
passed to theget_context_data
method are passed to the template for renderingSince you want to access a URL Parameter in
get_context_data
you would useself.kwargs
(self
is referring to yourView
class)