Hello,
I wanted to ask for some advice on a little issue I have with
implementing decorators in my views.
I have this piece of code, which I include in each view, to redirect
anonymous and staff users to a notice page (I don't want them to access
site members stuff):
def my_view(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(MYSTUFF_FORBIDDEN_URL)
if request.user.is_staff:
return HttpResponseRedirect(MYSTUFF_STAFF_URL)
# view logic and HttpResponse
I wanted how to implement a custom site decorator, based on this blog post:
http://siddhi.blogspot.com/2006/12/using-python-decorators-to-implement.html
Here's what I wrote:
from django.http import HttpResponseRedirect
def redirect_anonymous(fn):
def _check(request, *args, **kwargs):
try:
user = request.user.is_authenticated()
except KeyError:
return HttpResponseRedirect('/my-stuff/forbidden/')
return fn(args, kwargs)
return _check
def redirect_staff(fn):
def _check(request, *args, **kwargs):
if request.user.is_staff:
return HttpResponseRedirect('/my-stuff/staff/')
return fn(args, kwargs) # here's where I receive error
return _check
However when I modify my view function to add these checks as decorators:
@redirect_staff
@redirect_anonymous
def my_view(request):
#view logic and HttpResponse
and I log in to my site as a normal user, I receive an AttributeError
stating that:
'tuple' object has no attribute 'user'
How should I go about creating my decorators? Is there something I'm
doing wrong?
Patrick
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---