Decorators

Class based

permission_required decorator for generic classbased view from django 1.3

permission.decorators.classbase.get_object_from_classbased_instance(instance, queryset, request, *args, **kwargs)[source]

Get object from an instance of classbased generic view

Parameters:
  • instance (instance) – An instance of classbased generic view

  • queryset (instance) – A queryset instance

  • request (instance) – A instance of HttpRequest

Returns:

An instance of model object or None

Return type:

instance

permission.decorators.classbase.permission_required(perm, queryset=None, login_url=None, raise_exception=False)[source]

Permission check decorator for classbased generic view

This decorator works as class decorator DO NOT use method_decorator or whatever while this decorator will use self argument for method of classbased generic view.

Parameters:
  • perm (string) – A permission string

  • queryset (queryset or model) – A queryset or model for finding object. With classbased generic view, None for using view default queryset. When the view does not define get_queryset, queryset, get_object, or object then obj=None is used to check permission. With functional generic view, None for using passed queryset. When non queryset was passed then obj=None is used to check permission.

Examples

>>> @permission_required('auth.change_user')
>>> class UpdateAuthUserView(UpdateView):
...     pass

Function based

permission_required decorator for generic function view

permission.decorators.functionbase.get_object_from_date_based_view(request, *args, **kwargs)[source]

Get object from generic date_based.detail view

Parameters:

request (instance) – An instance of HttpRequest

Returns:

An instance of model object or None

Return type:

instance

permission.decorators.functionbase.get_object_from_list_detail_view(request, *args, **kwargs)[source]

Get object from generic list_detail.detail view

Parameters:

request (instance) – An instance of HttpRequest

Returns:

An instance of model object or None

Return type:

instance

permission.decorators.functionbase.permission_required(perm, queryset=None, login_url=None, raise_exception=False)[source]

Permission check decorator for function-base generic view

This decorator works as function decorator

Parameters:
  • perm (string) – A permission string

  • queryset (queryset or model) – A queryset or model for finding object. With classbased generic view, None for using view default queryset. When the view does not define get_queryset, queryset, get_object, or object then obj=None is used to check permission. With functional generic view, None for using passed queryset. When non queryset was passed then obj=None is used to check permission.

Examples

>>> @permission_required('auth.change_user')
>>> def update_auth_user(request, *args, **kwargs):
...     pass

Method based

permission_required decorator for generic classbased/functionbased view

permission.decorators.methodbase.permission_required(perm, queryset=None, login_url=None, raise_exception=False)[source]

Permission check decorator for classbased/functionbased generic view

This decorator works as method or function decorator DO NOT use method_decorator or whatever while this decorator will use self argument for method of classbased generic view.

Parameters:
  • perm (string) – A permission string

  • queryset (queryset or model) – A queryset or model for finding object. With classbased generic view, None for using view default queryset. When the view does not define get_queryset, queryset, get_object, or object then obj=None is used to check permission. With functional generic view, None for using passed queryset. When non queryset was passed then obj=None is used to check permission.

Examples

>>> # As method decorator
>>> class UpdateAuthUserView(UpdateView):
>>>     @permission_required('auth.change_user')
>>>     def dispatch(self, request, *args, **kwargs):
...         pass
>>> # As function decorator
>>> @permission_required('auth.change_user')
>>> def update_auth_user(request, *args, **kwargs):
...     pass

permission_required

Decorator module for permission

permission.decorators.permission_required.permission_required(perm, queryset_or_model=None, login_url=None, raise_exception=False)[source]

Permission check decorator for classbased/functional generic view

This decorator works as class, method or function decorator without any modification. DO NOT use method_decorator or whatever while this decorator will use self argument for method of classbased generic view.

Parameters:
  • perm (string) – A permission string

  • queryset_or_model (queryset or model) – A queryset or model for finding object. With classbased generic view, None for using view default queryset. When the view does not define get_queryset, queryset, get_object, or object then obj=None is used to check permission. With functional generic view, None for using passed queryset. When non queryset was passed then obj=None is used to check permission.

Examples

>>> # As class decorator
>>> @permission_required('auth.change_user')
>>> class UpdateAuthUserView(UpdateView):
...     pass
>>> # As method decorator
>>> class UpdateAuthUserView(UpdateView):
...     @permission_required('auth.change_user')
...     def dispatch(self, request, *args, **kwargs):
...         pass
>>> # As function decorator
>>> @permission_required('auth.change_user')
>>> def update_auth_user(request, *args, **kwargs):
...     pass

Note

Classbased generic view is recommended while you can regulate the queryset with get_queryset() method. Detecting object from passed kwargs may not work correctly.

Decorators utils

Decorator utility module

permission.decorators.utils.redirect_to_login(request, login_url=None, redirect_field_name='next')[source]

redirect to login