I have the following view function:
def gettingstarted_info(request):
"""
First page of gettingstarted after Registration.
"""
if request.user.is_authenticated():
if request.user.get_profile().getting_started_boolean:
return redirect('/home/')
else:
user = request.user
else:
username = request.session.get('username', False)
if not username:
return redirect('/login')
else:
user = User.objects.get(email=username)
# the main part of the view function #
I would like to convert the first part of the view function into an @ decorator, so I could have something like —
@gettingstarted_dispatch
def getting_started_info(request):
# the main part of the view function
I took a look at the docs on the decorator function but was having a bit of difficulty converting the function so I could use it as an @ decorator. The decorator should filter out unauthorized individuals and return the user variable (note this is not the same as request.user). How would I do this? Thank you.
I think this function should do the trick for you. 🙂
The function passes the user variable as the
userkeyword arg.Alternatively, if you prefer, user as the second arg…
Also, here’s a really nice in depth tutorial on how to do decorators. 🙂
(Part I) http://www.artima.com/weblogs/viewpost.jsp?thread=240808
(Part II) http://www.artima.com/weblogs/viewpost.jsp?thread=240845
(Part III) http://www.artima.com/weblogs/viewpost.jsp?thread=241209