How to write a custom decorator in django?

The problem is

@is_premium_user def sample_view: ....... ...... 

I want certain views to be available only to premium website users.
And how can I use this decorator for various applications in my project?

+44
python django decorator permissions
Mar 29 '11 at 7:23
source share
5 answers

You do not need to write your own decorator for this, since user_passes_test already included in Django.

And there is a fragment ( group_required_decorator ) that extends this decorator and which should be quite appropriate for your use case.

If you really want to write your own decorator, then there is a lot of good documentation online.

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

+45
Mar 29 '11 at 7:44
source share

We played with various links above and could not get them to work, and then came across this very simple one that I adapted. http://code.activestate.com/recipes/498217-custom-django-login_required-decorator/

 from django.http import HttpResponseRedirect def authors_only(function): def wrap(request, *args, **kwargs): profile = request.user.get_profile() if profile.usertype == 'Author': return function(request, *args, **kwargs) else: return HttpResponseRedirect('/') wrap.__doc__=function.__doc__ wrap.__name__=function.__name__ return wrap 
+37
May 25 '11 at 9:22
source share

Thanks arie, the answer helped a long way, but it did not work for me.

When I found this snippet, I got it to work correctly: http://djangosnippets.org/snippets/983/

This solution worked for me:

Helper function

This function has the advantage of being reused elsewhere, as a replacement when replacing user.is_authenticated . This can be, for example, shown as a template tag.

 def my_custom_authenticated(user): if user: if user.is_authenticated(): return user.groups.filter(name=settings.MY_CUSTOM_GROUP_NAME).exists() return False 

Decorator

I just put this on top of my views.py since it is so short.

 def membership_required(fn=None): decorator = user_passes_test(my_custom_authenticated) if fn: return decorator(fn) return decorator 

Using

 @membership_required def some_view(request): ... 
+5
Mar 29 '13 at 15:06
source share

http://www.makina-corpus.org/blog/permission-required-decorator-django

i based on my blog.

Stick to this file in the python path or in your util application and import it into the views:

eg

 project_dir |_ app1 |_ app2 |_ utils |_ __init__.py |_ permreq.py from util.permreq import permission_required @permmission_required('someapp.has_some_perm', template='denied.html') def some_view(request): blah blah 
+1
Mar 29 '11 at 7:43
source share

See examples in the django itself:

http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/decorators.py

Your specific example is most likely the version of user_passes_test, where the test will belong to the premium group.

To use anywhere, make a python package and import it from there. While it is on your sys.path, it will be found.

0
Mar 29 '11 at 7:45
source share



All Articles