Jay P.
Jay P.

Reputation: 2590

How to limit urls to admin users in Django

I'm currently limiting some urls to only logged-in users in Django by doing the following.

urls.py

urlpatterns = [
    url(r'^$', login_required(views.MainView.as_view()), name='index')
]

Is there a way to limit urls to only admin and staff users except for regular users?

Upvotes: 1

Views: 73

Answers (1)

Ralf
Ralf

Reputation: 16485

You can look into these two:

  1. user_passes_test decorator or also UserPassesTestMixin
  2. permission_required decorator or also the PermissionRequiredMixin

They should be sufficient for your needs. The docs show a few examples that fit your use case.

Upvotes: 1

Related Questions