Skip to main content

UserPassesTestMixin

Deny a request with a permission error if the test_func() method returns False.

Methods


test_func()

@classmethod
def test_func() - > boolean

Raises a NotImplementedError to ensure subclasses provide their own logic for determining if a user has permission to access the view.

Returns

TypeDescription
booleanA boolean value indicating whether the user passes the defined permission test.

get_test_func()

@classmethod
def get_test_func() - > callable

Override this method to use a different test_func method.

Returns

TypeDescription
callableThe function or method that will be executed to perform the permission check.

dispatch()

@classmethod
def dispatch(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
*args: tuple,
**kwargs: dict
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Executes the permission test before processing the request; if the test fails, it triggers the handle_no_permission logic.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The current HTTP request object being processed.
*argstuplePositional arguments passed to the view.
**kwargsdictKeyword arguments passed to the view.

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The standard HTTP response from the parent dispatch method or the result of handle_no_permission if the test fails.