0

对于我的 django 应用程序中的每个用户,我都会生成一个我想提供的静态 .ical 文件。是否有 CBV 以及我将覆盖哪种方法?由于我的所有视图都是基于类的,因此我宁愿不使用带有 aHttpResponse@auth_required装饰器的基于函数的视图: Django: Serving a Download in a Generic View

4

1 回答 1

1

只需从View模型继承并覆盖视图方法。

class ICalDownload(View):
    def get(self, *args, **kwargs):
        # return your response just like you would in a function view.

如果你想保护视图,我喜欢使用django-braces。否则,您需要method_decorator在 dispatch 方法上使用:

@method_decorator(auth_required)
def dispatch(self, *args, **kwargs):
    return super(ICalDownload, self).dispatch(*args, **kwargs)

在这一点上,基于函数的视图可能会更简单一些,但是像你一样,我喜欢总是使用基于类的视图。

于 2013-11-04T00:55:24.193 回答