views:

69

answers:

2

I have a few restricted areas on the site, for which I would like to specify login_required decorator. However I would like to do that once per inclusion in main urls.py, not per individual url in included urls.py

So instead of:

/private/urls.py:

(r'^profile/$', login_required(profile)),

I'd do something along the lines:

/urls.py

urlpatterns = patterns('',
                      ...
                      (r'^private/', login_required(include('private'))),
                      )

Except that it doesn't work, unfortunately.

+2  A: 

login_required is meant for wrapping view callable, not include(), and looking at source code:

http://code.djangoproject.com/browser/django/tags/releases/1.1.1/django/conf/urls/defaults.py#L9

-- I don't think there is an easy way to use default (or even custom) login_required with include() to achieve what you want to achieve.

Writing this, I think that the reasonable approach would be to use some "login required middleware", like this one: http://www.djangosnippets.org/snippets/1179/ and forget about decorating urls in urls.py.

Tomasz Zielinski
A: 

@login_required is applied against views, not urlconf. You'll need to apply it against the views in question, either in your own module or via monkeypatching.

Ignacio Vazquez-Abrams