如何要求登录Django Generic Views? [英] How to require login for Django Generic Views?

查看:107
本文介绍了如何要求登录Django Generic Views?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想限制对Django Generic Views处理的URL的访问。

I want to restrict access to URLs handled by Django Generic Views.

对于我的观点,我知道 login_required
另外创建/删除/更新通用视图采用 login_required 参数,但是我找不到其他通用视图的方法。

For my Views I know that login_required decorator does the job. Also Create/Delete/Update Generic Views take the login_required argument, but I couldn't find a way to do this for other Generic Views.

推荐答案

对于Django< 1.5,您可以通过在urls中包装函数来添加一个装饰器,这样可以包含一般视图:

For Django < 1.5, you can add a decorator by wrapping the function in your urls, which allows you to wrap the generic views:

from django.contrib.auth.decorators import login_required
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
    (r'^foo/$', login_required(direct_to_template), {'template': 'foo_index.html'}),
    )

基于Django 1.4的通用视图已被弃用,并在Django 1.5中被删除。但是同样的原则也适用,只需使用 login_required 装饰器包装基于视图的视图函数:

The function-based generic views are deprecated in Django 1.4 and were removed in Django 1.5. But the same principle applies, just wrap the view function of the class based view with the login_required decorator:

login_required(TemplateView.as_view(template_name='foo_index.html'))

这篇关于如何要求登录Django Generic Views?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆