tags:

views:

342

answers:

3

I want to restrict access to urls serves by django generic views. For my views i know that login_required decorator does the job. Also Create/update/delete generic views takes login_requied argument but I couldn't find a way to do this for other generic views.

A: 

Use the following:

from django.contrib.auth.decorators import login_required

@login_required
def your_view():
    # your code here
Aamir hussain
+9  A: 

You can also adds 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'}),
    )
Will Hardy
+5  A: 

If you don't want to write your own thin wrapper around the generic views in question (as Aamir suggested), you can also do something like this in your urls.py file:

from django.conf.urls.defaults import *

# Directly import whatever generic views you're using and the login_required
# decorator
from django.views.generic.simple import direct_to_template
from django.contrib.auth.decorators import login_required

# In your urlpatterns, wrap the generic view with the decorator
urlpatterns = patterns('',
    (r'', login_required(direct_to_template), {'template': 'index.html'}),
    # etc
)
Will McCutchen