Search code examples
djangogetrequesttypeerrordjango-context

Can I add more than two arguments to get_context_data()?


I get this error

TypeError at /debate/1/
get_context_data() takes exactly 2 arguments (1 given)

Right now it is defined as:

 def get_context_data(self, **kwargs):

And I want it to be:

 def get_context_data(self, request, **kwargs):

so I can do inside:

sort_by = request.GET.get('sort', '-rating_score')

Is this a good idea, and how to do it?


Solution

  • Request object is available as member of a Class Based View object and can be accessed by self.request. If, for some reason, you do pass it in **kwargs use kwargs.get("request") since it's a plain dict.