类对象属性的`get_context_data`

Alg*_*bra 2 python django

get_context_data类对象的属性的。

PasswordContextMixindjango / contrib / auth / views.py中遇到

class PasswordContextMixin:
    extra_context = None

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context.update({
            'title': self.title,
            **(self.extra_context or {})
        })
        return context
Run Code Online (Sandbox Code Playgroud)

我感到困惑context = super().get_context_data(**kwargs),因为它等于context = object.get_context_data(**kwargs)

 In [15]: getattr(object, 'get_context_data')
AttributeError: type object 'object' has no attribute 'get_context_data'
Run Code Online (Sandbox Code Playgroud)

如何理解呢?

Dan*_*man 5

顾名思义,此类是一个mixin。它旨在与视图类(特别是TemplateView的子类)一起使用,该类将定义get_context_data

  • @中岛/sf/ask/37354201/ (2认同)