问题:在通用类视图类的A方法中动态添加一个属性,如self.username,在B方法中访问该属性却报属性错误,推断是通用视图类会不停地创建和销毁。查询django文档如下,可知的确是每次url在分配view的时候都会创建不同的对象。通过日志输出,如下图,也证明对象的确不同。
Because Django’s URL resolver expects to send the request and associated arguments to a callable function, not a class, class-based views have an as_view() class method which serves as the callable entry point to your class. The as_view entry point creates an instance of your class and calls its dispatch() method. dispatch looks at the request to determine whether it is a GET, POST, etc, and relays the request to a matching method if one is defined, or raises HttpResponseNotAllowed if not: