Search code examples
djangopylint

Django - Is overriding TemplateView.get with different signature a bad practice?


I wrote a Django application with a JobView inherited from TemplateView. The get method needs an additional argument (job_id) which is in the URL. Basically, my urls.py looks like this:

# urls.py
from django.conf import url
from .views import JobView

urlpatterns = [
    url(r'^job/(?P<job_id>[0-9]+)$',
        JobView.as_view())

The views.py contains the definition of JobView:

# views.py
from django.views.generic import TemplateView
from django.http import HttpResponse

class JobView(TemplateView):
    def get(self, request, job_id):
        # Some stuff
        return HttpResponse("something")

When checking this code with pylint, I get this error: [arguments-differ] Arguments number differs from overriden 'get' method.

To fit the TemplateView.get method, the definition of JobView.get should be:

class JobView(TemplateView):
    def get(self, request):
        # Some stuff

Is overriding with an other signature a bad practice? If yes, how am I supposed to implement such a thing?


Solution

  • The signature should be the same, but you can add custom args and kwargs if you pop them when calling the super class method. The actual method signature of a View (subclass) is get(self, request, *args, **kwargs).