django-cbv提供密码保护的静态内容

时间:2013-11-02 20:07:33

标签: python django static

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

1 个答案:

答案 0 :(得分: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

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

此时,基于函数的视图可能会更简单一些,但与您一样,我喜欢始终使用基于类的视图。