How can make Django permission_required decorator not to redirect already logged-in users to login page, but display some message

dsmilkov picture dsmilkov · Aug 9, 2009 · Viewed 9.1k times · Source

How can make Django permission_required decorator not to redirect already logged-in users to login page, but display some message like Insufficient permissions?

Thank you.

Answer

Manoj Govindan picture Manoj Govindan · Aug 9, 2009

A quick and dirty solution would be to write your own decorator to do this. Something like this:

decorator_with_arguments = lambda decorator: lambda *args, **kwargs: lambda func: decorator(func, *args, **kwargs)

@decorator_with_arguments
def custom_permission_required(function, perm):
    def _function(request, *args, **kwargs):
        if request.user.has_perm(perm):
            return function(request, *args, **kwargs)
        else:
            request.user.message_set.create(message = "What are you doing here?!")
            # Return a response or redirect to referrer or some page of your choice
    return _function

You can then decorate your view thus:

@custom_permission_required('my_perm')
def my_view(request, *args, **kwargs):
    #Do stuff