Karesh Arunakirinathan Karesh Arunakirinathan - 1 year ago 138
Python Question

Django staff login to all urls

I want to define some specific urls starts with /staff/* to access only by staff. So only staffs can access the urls starts with /staff/*

How can I define that in Django ?

Answer Source

Use a custom middleware. If the url starts with /staff/ and request.user is not staff, raise Http404 or return some special message to client.

Below is an example:

class StaffCheckMiddleware(object):
    def process_request(self, request):
        full_path = request.get_full_path()
        if full_path.startswith('/staff/') and not request.user.is_staff:
            raise Http404      

Then add StaffCheckMiddlware in settings.py.

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