Py学习  »  Django

[精华] 针对通用视图,如何使用@login_required

Leo • 11 年前 • 9048 次点击  

针对ListView/DetailView等视图,如何使用@login_required?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/577
文章 [ 2 ]  |  最新文章 11 年前
Leo
Reply   •   1 楼
Leo    11 年前

太感谢了,正为这个发愁,本来准备换函数视图的~~~

Py站长
Reply   •   2 楼
Py站长    11 年前

最简单的方式是采用以下官方推荐的方式

Decorating in URLconf The simplest way of decorating class-based views is to decorate the result of the as_view() method. The easiest place to do this is in the URLconf where you deploy your view:

from django.contrib.auth.decorators import login_required, permission_required
from django.views.generic import TemplateView

from .views import VoteView

urlpatterns = patterns('',
    (r'^about/', login_required(TemplateView.as_view(template_name="secret.html"))),
    (r'^vote/', permission_required('polls.can_vote')(VoteView.as_view())),
)

https://docs.djangoproject.com/en/1.6/topics/class-based-views/intro/

另外,你可以看看这里 http://stackoverflow.com/questions/10275164/django-generic-views-using-decorator-login-required