Django 基于类的通用视图和身份验证

发布于 2024-11-18 22:28:01 字数 715 浏览 5 评论 0原文

我对 Django 还很陌生(从 1.3 开始)。在构建应用程序时,我从第一天开始就使用新的基于类的通用视图,结合使用内置类并在需要添加到上下文的地方对它们进行子类化。

现在我的问题是,我需要返回我的视图,并让它们仅可供登录用户访问。我找到的所有文档都显示了如何使用旧的功能通用视图来执行此操作,但不使用基于类的视图。

这是一个示例类:

class ListDetailView(DetailView):
    context_object_name = "list"

    def get_queryset(self):
        list = get_object_or_404(List, id__iexact=self.kwargs['pk'])
        return List.objects.all()

    def get_context_data(self, **kwargs):
        context = super(ListDetailView, self).get_context_data(**kwargs)
        context['subscriber_list'] = Subscriber.objects.filter(lists=self.kwargs['pk'])
        return context

How do I add authentication to django's new class-based views?

I am pretty new to Django (starting with 1.3). In building an app, I went with the new class-based generic views from day one, using a combination of the built in classes and subclassing them where I needed to add to the context.

Now my problem is, I need to go back to my views, and have them accessible only to logged in users. ALL the documentation I have found shows how to do this with the old functional generic views, but not with class-based.

Here is an example class:

class ListDetailView(DetailView):
    context_object_name = "list"

    def get_queryset(self):
        list = get_object_or_404(List, id__iexact=self.kwargs['pk'])
        return List.objects.all()

    def get_context_data(self, **kwargs):
        context = super(ListDetailView, self).get_context_data(**kwargs)
        context['subscriber_list'] = Subscriber.objects.filter(lists=self.kwargs['pk'])
        return context

How do I add authentication to django's new class-based views?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就此别过 2024-11-25 22:28:01

还有一个身份验证混合选项,您可以从中派生视图类。因此,使用 来自 brack3t.com 的 mixin

class LoginRequiredMixin(object):

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

然后您可以创建新的“需要身份验证”视图,如下所示

from django.views.generic import DetailView

class MyDetailView(LoginRequiredMixin, DetailView):
    ....

:需要其他补充。感觉很像不重复自己。

There's also the option of an authentication mixin, which you would derive your view class from. So using this mixin from brack3t.com:

class LoginRequiredMixin(object):

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

you could then create new "authentication required" views like this:

from django.views.generic import DetailView

class MyDetailView(LoginRequiredMixin, DetailView):
    ....

with no other additions needed. Feels very much like Not Repeating Oneself.

花之痕靓丽 2024-11-25 22:28:01

文档中有一个关于装饰的部分基于类的视图——如果您只想使用旧的login_required等,那就是正确的方法。

There's a section in the docs on decorating class-based views -- if you just want to use the old login_required etc., that's the way to go.

川水往事 2024-11-25 22:28:01

我正在描述一种装饰任何 ListView 的方法:

class MyListView(ListView):
    decorator = lambda x: x

    @method_decorator(decorator)
    def dispatch(self, request, *args, **kwargs):
       return super(MyListView, self).dispatch(request, *args, **kwargs)

在编写这样的基于类的视图之后,
您可以直接将任何基于函数的装饰器插入到 url 中。

url(r'^myurl/
, MyListView.as_view(decorator=login_required))

I am describing a method to decorate any ListView :

class MyListView(ListView):
    decorator = lambda x: x

    @method_decorator(decorator)
    def dispatch(self, request, *args, **kwargs):
       return super(MyListView, self).dispatch(request, *args, **kwargs)

After writing a class based view like this,
you can directly insert any function based decorator into the url as so.

url(r'^myurl/
, MyListView.as_view(decorator=login_required))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文