ListView调用`get_template_names`而不调用它

时间:2018-05-03 02:27:42

标签: django

我正在阅读ListView的Django源代码: django/list.py

class MultipleObjectTemplateResponseMixin(TemplateResponseMixin):
    template_name_suffix = '_list'
    def get_template_names(self):
       try:
            names = super().get_template_names()

class ListView(MultipleObjectTemplateResponseMixin, BaseListView):
    """
    Render some list of objects, set by `self.model` or `self.queryset`.
    `self.queryset` can actually be any iterable of items, not just a queryset.
    """

当我定义ListView时,template_name会自动分配

class IndexView(generic.ListView):
    pass

我认为MultipleObjectTemplateResponseMixin中的分配步骤应该是

class MultipleObjectTemplateResponseMixin(TemplateResponseMixin):
    template_name_suffix = '_list'
    def get_template_names(self):
       try:
            names = super().get_template_names()
            ....
    template_name = self.get_template_names()

如何调用get_template_names而不调用它?

1 个答案:

答案 0 :(得分:1)

get_template_names类的render_to_response方法中调用

TemplateResponseMixin,该方法是MultipleObjectTemplateResponseMixin的超类。