Sunday, November 30, 2014

Re: simplifying double decorators?

Hi,

def my_double_decorator(view):
    view
= user_passes_test(some_test_function, login_url='/', redirect_field_name=None)(view)
   
return login_required()(view)

Collin


On Sunday, November 30, 2014 1:31:12 PM UTC-5, Richard Brockie wrote:
Hi,

I'm running into the situation where I have several views with the same set of decorators:

@login_required()
@user_passes_test(some_test_function, login_url='/', redirect_field_name=None)
def some_view(request):
    # some code
    return render(request, 'some_template.html', locals())

How would I go about combining the two (or more) decorators into a single decorator that can be used instead and retain the functionality?

IE: this:

@login_required()
@user_passes_test(some_test_function, login_url='/', redirect_field_name=None)
def some_view...

becomes abstracted to something like this:

@my_custom_decorator_with_redirects()
def some_view...


--
    R.

Richard Brockie

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b67a07f2-6a18-46a8-a418-05afc23e73f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment