Thanks a lot Bruno! That really helped :)
On Nov 17, 3:20 am, bruno desthuilliers <[EMAIL PROTECTED]> wrote: > On 17 nov, 10:02, TH <[EMAIL PROTECTED]> wrote: > > > > > Hello, > > > I wanted to redirect a logged in user if he access certain page. For > > example i do not want the user to access registration page if he is > > logged in. > > > A simple way to do this is: > > > def register(request): > > if request.user.is_authenticated(): > > return HttpResponseRedirect('/ > > some_nice_page_for_logged_in_user') > > ... > > > The problem is i have lot of pages that only anonymous user can > > access. So i wanted to write a decorator so that i can do something > > like that > > > @anonymous_only > > def register(request): > > A "decorator" is just a function (or any other callable) that takes a > function as argument and returns a function (or any other callable). > The @decorator syntax is only syntactic sugar. > > In most cases, the returned function is just a wrapper around the > decorated one, and this is obviously what you want: > > def anonymous_only(view): > # 'view' is the view function to decorate > # since we decorate views, we know the first parameter > # is always going to be the current request > # > # '_wrapper' is the function that will be used instead > # of 'view'. > def _wrapper(request, *args, **kw): > # here we can access the request object and > # either redirect or call 'view' > if request.user.is_authenticated(): > return HttpResponseRedirect('/page_for_logged_in_user') > return view(request, *args, **kw) > > # this part is not mandatory, but it may help debugging > _wrapper.__name__ = view.__name___ > _wrapper.__doc__ = view.__doc__ > > # and of course we need to return our _wrapper so > # it replaces 'view' > return _wrapper > > Note that this example assume you always want to redirect to the same > page if the user is logged in, which might not be the case. Things get > a bit more complicated if you want your decorator to takes the > redirection url as param, since we'll then need one more indirection, > IOW : a function that takes an url and returns a function that takes a > function and returns a 'wrapper': > > def anonymous_only(redirect_to): > def deco(view): > def _wrapper(request, *args, **kw): > if request.user.is_authenticated(): > return HttpResponseRedirect(redirect_to) > return view(request, *args, **kw) > > _wrapper.__name__ = view.__name___ > _wrapper.__doc__ = view.__doc__ > return _wrapper > > return deco > > Then you use it that way: > > @anonymous_only('/page_for_logged_in_user') > def register(request): > # code here > > HTH --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---