1

我有以下装饰器:

def my_decorator(setting_value):

    def actual_decorator(func):
        func.my_setting = setting_value
        return func

    return actual_decorator

我正在尝试my_setting在这样的上下文处理器中阅读:

# Resolve the url
try:
    page_resolve = resolve(request.path_info)
except Resolver404:
    return {}

# Get the page view function
page_view = page_resolve.func

if (page_view.my_setting):
    return extra_context_data
else
    return {}

当使用 CBV 时,整个方案就崩溃了。如何使装饰器与 FBV 和 CBV 兼容?

4

1 回答 1

0

将装饰器应用于dispatch功能:

class MyView(View):
    @my_decorator
    def dispatch(self, *args, **kwargs):
        return super(MyView, self).dispatch(*args, **kwargs)
于 2012-10-15T09:36:53.223 回答