0

Referencece to response_class in Django's code: django/base.py

class TemplateResponseMixin:
    """A mixin that can be used to render a template."""
    response_class = TemplateResponse
   def render_to_response(self, context, **response_kwargs):
       response_kwargs.setdefault('content_type', self.content_type)
    #here
        return self.response_class(
     #here
            request=self.request,
            template=self.get_template_names(),
            context=context,
            using=self.template_engine,
            **response_kwargs
        )

The class attribute setting response_class = TemplateResponse,
while call it through instance's attribute self.response_class,
I guess it might be super().response_class

How to understand it?

AbstProcDo
  • 19,953
  • 19
  • 81
  • 138

1 Answers1

1

You need to use super() when calling superclass's method. But in case of response_class it's just attribute defined inside TemplateResponseMixin so you can simple accessed it through self.response_class. Since response_class is Class to instancinate you need to add () like this: self.response_class(*args, **kwargs). You can check this question to get more details about super().

Example:

class A:
    def method_a(self):
        pass

class B(A):
    some_class = SomeClass

    def method_a(self):
        super().method_a() # This code will find A's method_a and call it
        self.some_class() # This will only instancinate some_class attribute of current B's instance
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100