Mixins
Added in version 1.0.4
LoginRequiredMixin
A login required mixin for use with class-based views.
This Class is a light wrapper around the Django login_required decorator,
function parameters are instead attributes defined on the class.
Due to Python Method Resolution Order (MRO), this mixin must be added as the left most mixin of a view.
Attributes:
| Name | Type | Description |
|---|---|---|
redirect_field_name |
str
|
Default: |
login_url |
str
|
Default: |
Example
from guardian.mixins import LoginRequiredMixin
from django.views.generic import View
class SecretView(LoginRequiredMixin, View):
redirect_field_name = 'foobar'
login_url = '/let-me-in/'
def get(self, request):
return HttpResponse('secret-view')
Note
The mixin has exactly the same flow as login_required decorator:
-
If the user isn't logged in, redirect to
settings.LOGIN_URL, passing the current absolute path in the query string.- Example:
/accounts/login/?next=/polls/3/.
- Example:
-
If the user is logged in, execute the view normally. The view code is free to assume the user is logged in.
See Also
Source code in guardian/mixins.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
PermissionRequiredMixin
A view mixin that verifies if the current logged-in user has the specified permission.
This mixin works by wrapping the request.user.has_perm(...) method.
If a get_object() method is defined either manually or by including
another mixin (e.g., SingleObjectMixin) or self.object is
defined, then the permission will be tested against that specific instance.
Alternatively, you can specify get_permission_object() method if self.object
or get_object() does not return the object against you want to test permission
The mixin does the following:
-
If the user isn't logged in, redirect to settings.LOGIN_URL, passing the current absolute path in the query string.
- Example: /accounts/login/?next=/polls/3/.
-
If
raise_exceptionis set toTrueaPermissionDenied(403) is raised instead of redirecting the user tosettings.LOGIN_URL. -
If the user is logged in, and passes the permission check, the view is executed normally.
Note
Testing of a permission against a specific object instance requires an
authentication backend that supports.
The guardian.backends.ObjectPermissionBackend or a custom implementation
must be listed under the list of authentication backends in your project.
Attributes:
| Name | Type | Description |
|---|---|---|
permission_required |
str | list[str]
|
permissions to check
in form |
login_url |
str
|
Default: |
redirect_field_name |
str
|
Default is |
return_403 |
bool
|
Returns 403 error page instead of redirecting.
Default is |
return_404 |
bool
|
Returns 404 error page instead of redirecting.
Default is |
raise_exception |
bool
|
Default is |
permission_denied_message |
str
|
A string to pass to the |
accept_global_perms |
bool
|
Whether the mixin should first check for global perms.
If none are found, proceed to check object level permissions.
Default is |
permission_object |
None | object
|
Object against which test the permission;
if not set fallback to |
any_perm |
bool
|
Whether any of the permissions in sequence is accepted.
Default is |
Example
from guardian.mixins import PermissionRequiredMixin
from django.views.generic import View
class SecureView(PermissionRequiredMixin, View):
...
permission_required = 'auth.change_user'
...
Source code in guardian/mixins.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
check_permissions(request)
Check if the user has the required permissions.
Checks if request.user has all permissions returned by the
get_required_permissions() method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
HttpRequest
|
The original request. |
required |
Source code in guardian/mixins.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
get_object_permission_denied_message()
Get the message to pass to the PermissionDenied exception.
Override this method to override the object_permission_denied_message attribute.
Source code in guardian/mixins.py
147 148 149 150 151 152 | |
get_required_permissions(request=None)
Get the required permissions.
Returns list of permissions in format permission_required attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
HttpRequest
|
Original request. |
None
|
Source code in guardian/mixins.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
on_permission_check_fail(request, response, obj=None)
Method called upon permission check fail.
Allow subclasses to hook into the permission check failure process. By default, it does nothing and should only be overridden, if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
HttpRequest
|
Original request |
required |
response
|
HttpResponse
|
403 response returned by check_permissions method. |
required |
obj
|
Model | Any
|
Object that was fetched from the view (using |
None
|
Source code in guardian/mixins.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
PermissionListMixin
A view mixin that filter a queryset by user and permission.
This mixin filter object retrieved by a queryset that the logged-in user has the specified permission for.
Example
from django.views.generic import ListView
from guardian.mixins import PermissionListMixin
class SecureView(PermissionListMixin, ListView):
...
permission_required = 'articles.view_article'
...
# or
class SecureView(PermissionListMixin, ListView):
...
permission_required = 'auth.change_user'
get_objects_for_user_extra_kwargs = {'use_groups': False}
...
Attributes:
| Name | Type | Description |
|---|---|---|
permission_required |
str | list[str]
|
permissions to check
in format: |
get_objects_for_user_extra_kwargs |
dict
|
Extra params to pass to |
Source code in guardian/mixins.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | |
get_get_objects_for_user_kwargs(queryset)
Get kwargs to pass to get_objects_for_user.
Returns:
| Type | Description |
|---|---|
dict
|
dict of kwargs to be passed to |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryset
|
QuerySet
|
Queryset to filter. |
required |
Deprecation Warning
This method is deprecated and will be removed in future versions.
Use get_user_object_kwargs instead which has identical behavior.
Source code in guardian/mixins.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
get_required_permissions(request=None)
Get the required permissions.
Returns list of permissions in format permission_required attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
HttpRequest
|
Original request. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of the required permissions. |
Source code in guardian/mixins.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
get_user_object_kwargs(queryset)
Get kwargs to pass to get_objects_for_user.
Returns:
| Type | Description |
|---|---|
dict
|
dict of kwargs to be passed to |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queryset
|
QuerySet
|
Queryset to filter. |
required |
Source code in guardian/mixins.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |