如何在我的URL中使用login_required装饰器? [英] How do I use the The login_required decorator in my URL?
问题描述
我想检查用户是否被授权使用某些URL。我正在使用通用视图。
I want to check that the user is authorized for certain URLs. I'm using generic views.
这里的文档表示login_required可以作为可选参数传递,但我不确定。这样可能是:(r'^ $','archive_index',link_info_dict,'coltrane_link_archive_index',login_required = True,),
The docs here say the login_required can be passed as an optional arguments but I'm not sure. Something like this maybe: (r'^$', 'archive_index', link_info_dict, 'coltrane_link_archive_index', login_required=True,),
我有这个,我想要能够使用URL中的login_required装饰器。可能吗?我如何做?
I have this and I would like to be able to use the login_required decorator within the URL. Is it possible? How can I do it?
from django.conf.urls.defaults import *
from coltrane.models import Link
link_info_dict = {
'queryset': Link.live.all(),
'date_field': 'pub_date',
}
urlpatterns = patterns('django.views.generic.date_based',
(r'^$', 'archive_index', link_info_dict, 'coltrane_link_archive_index'),
(r'^(?P<year>\d{4})/$', 'archive_year', link_info_dict, 'coltrane_link_archive_year'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/$', 'archive_month', link_info_dict, 'coltrane_link_archive_month'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/$', 'archive_day', link_info_dict, 'coltrane_link_archive_day'),
(r'^(?P<year>\d{4})/(?P<month>\w{3})/(?P<day>\d{2})/(?P<slug>[-\w]+)/$', 'object_detail', link_info_dict, 'coltrane_link_detail'),
)< h2>
例如,我如何保护这个页面(没有看到添加login_Required装饰器)?
)
For example how would I protect this page here(there's no view to add the login_Required decorator to)?
(r'^$', 'django.views.generic.simple.direct_to_template', {
'template': 'home.html'
}, ),
推荐答案
要在urls.py中使用装饰器,您需要使用真正的功能而不是名称:
To use decorators in urls.py you need use real functions instead of their names:
from django.contrib.auth.decorators import login_required
import django.views.generic.date_based as views
urlpatterns = patterns('',
(r'^$', login_required(views.archive_index), link_info_dict,
'coltrane_link_archive_index'),
...
这篇关于如何在我的URL中使用login_required装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!