0

为什么我们在这里有 template_name = None 作为类变量?(来自 django 源代码)

这是因为如果 self.template_name 为 None,会引发错误吗?
(self.template_name 会查找实例变量,如果它不存在,则返回类变量)
如果是这样,最好有def __init__(self): self.template_name = None ?

class TemplateResponseMixin(object):
    """                                                                                                                                                                                                                                                                       
    A mixin that can be used to render a template.                                                                                                                                                                                                                            
    """
    template_name = None
    response_class = TemplateResponse

    def render_to_response(self, context, **response_kwargs):
        """                                                                                                                                                                                                                                                                   
        Returns a response with a template rendered with the given context.                                                                                                                                                                                                   
        """
        return self.response_class(
            request = self.request,
            template = self.get_template_names(),
            context = context,
            **response_kwargs
        )

    def get_template_names(self):
        """                                                                                                                                                                                                                                                                   
        Returns a list of template names to be used for the request. Must return                                                                                                                                                                                              
        a list. May not be called if render_to_response is overridden.                                                                                                                                                                                                        
        """
        if self.template_name is None:
            raise ImproperlyConfigured(
                "TemplateResponseMixin requires either a definition of "
                "'template_name' or an implementation of 'get_template_names()'")
        else:
            return [self.template_name]
4

1 回答 1

0

TemplateResponseMixin 是一个不使用 init 的 mixin,以便在多重继承中更容易使用。它不需要自己的状态,因此不需要构造函数。这也使继承更容易,因为您不需要在子类中对其调用构造函数。

The template_name is set as a class instance obviously because there is no constructor. It implies that it should be set from the subclass. Also, changing the value of it will affect all future instances of that mixin.

于 2013-01-06T05:40:00.457 回答