Django staff login to all urls

django, django-views, python, python-2.7

Solution

You can use `user_passes_test` or `staff_member_required` decorator for the view that you associate with your url (that starts with `/staff/`), an example might be as follows:

With `user_passes_test` decorator:

from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.is_staff, login_url='/some_url/')
def your_view(request, ...):
    # Only for staff

With `staff_member_required` decorator:

from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def your_view(request, ...):
    # Only for staff

Problem

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 ?

Original source