让我们用一个表示Django控制器的类,其中一个方法称为_onSuccess
:
class ConfirmController(object):
...
def _onSuccess(self, controller):
...
该类稍后将实例化为:
^{pr2}$我正在尝试使用ConfirmController的子类:
class ConfirmControllerEx(ConfirmController):
def _onSuccess(self, controller):
# shortened to demonstrate even simple call to super
# causes a different behaviour
super(ConfirmControllerEx, self)._onSuccess(controller)
我可能在python学习中错过了一些东西,但是谁能解释一下为什么上面的子类_onSuccess
与原始方法等价?
如果我确实使用了上面的子类ConfirmControllerEx
:
def credit_confirm_info(request, payment_module, template='/some/template.html'):
controller = ConfirmControllerEx(request, payment_module)
controller.confirm() # this method calls self._onSuccess
return controller.response
credit_confirm_info = never_cache(credit_confirm_info)
我得到了NoneType has no method has_header
错误,就像credit_confirm_info
被再次调用,但是request
参数等于None
。在
我希望子类和子类方法_onSuccess
与对super
的普通调用不会与原始方法不同。我是不是少了点什么?在
更新(异常回溯):
Traceback:
File "/home/dunric/Projects/Example.com/satchmo/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/dunric/Projects/Example.com/satchmo/gastroceny_cz/localsite/views.py" in cod_confirm_info
279. template='shop/checkout/cod/confirm.html')
File "/home/dunric/Projects/Example.com/satchmo/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
90. add_never_cache_headers(response)
File "/home/dunric/Projects/Example.com/satchmo/lib/python2.7/site-packages/django/utils/cache.py" in add_never_cache_headers
129. patch_response_headers(response, cache_timeout=-1)
File "/home/dunric/Projects/Example.com/satchmo/lib/python2.7/site-packages/django/utils/cache.py" in patch_response_headers
119. if not response.has_header('Last-Modified'):
Exception Type: AttributeError at /checkout/cod/confirm/
Exception Value: 'NoneType' object has no attribute 'has_header'
我不知道django的具体情况,但是这个方法:
与父类的
_onSuccess
等价。它通过super
调用父实现,但它忽略该调用返回的任何内容,只返回None
(隐式地,通过执行到达方法定义的末尾)。如果您稍后得到一个错误,它似乎表明您有一个None
对象(NoneType
)的实例,那么这就是我对错误的猜测。但是,如果_onSuccess
方法的约定总是返回None
,那就不可能了。在相关问题 更多 >
编程相关推荐