Tuesday, October 11, 2011

Sample Custom Decorator

Hey Guys,

Would anyone be willing to show me an example of a very simple and
dumb decorator for views? I've been trying to read the existing
decorators and play with a couple of snippets but I'm having a lot of
trouble with two aspects -- grabbing the User Instance and Redirecting
somewhere besides Login (without making it look like a hack).

Here's some ugly pseudo-code for what I'm trying to accomplish...

# Custom Decorator
def my_decorator(function = None)

# Grab Data
request = how_do_i_get_this?
user = request.user

# Perform Logic, Redirect or Continue Normally
if user.foo():
redirect to '/foo' URL
else
display requested view?


# Decorated TemplateView
url(r'^$', my_decorator(TemplateView.as_view(template_name =
'bar.html'))),

Thanks!

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment