社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

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

Leo • 10 年前 • 8583 次点击  

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

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

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

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

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

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