user677990 user677990 - 1 year ago 174
Python Question

How to write a custom decorator in django?

The problem -

def sample_view:

I want certain views accesible to only the premium users of the website.

And how can I use this decorator across various applications in my project?

Answer Source

You don't have to write your own decorator for this as user_passes_test is already included in Django.

And there's a snippet (group_required_decorator) that extends this decorator and which should be pretty appropriate for your use case.

If you really want to write your own decorator then there's a lot of good documentation on the net.

And well, to (re-) use the decorator just put your decorator in a module on your path and you can import it from any other module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download