djblets.views.generic.base¶
Base classes and mixins for new class-based generic views.
- class CheckRequestMethodViewMixin¶
Bases:
object
Generic view mixin to check the HTTP method before dispatching.
In a normal class-based view, it’s up to the
dispatch()
method to ensure that an HTTP method is valid before dispatching to the handler. However, more complex views may need to perform other checks before dispatching, meaning that logic is being performed before the validity checks happen, which would be unnecessary and wasteful.This mixin, when put before any other mixins or parent classes, will perform the validity checks up-front and immediately bail with a HTTP 405 Method Not Allowed if the HTTP method is not allowed.
- dispatch(request, *args, **kwargs)¶
Dispatch a HTTP request to the right handler.
This will first check if the HTTP method is allowed. If it is not, then this will immediately return a HTTP 405 Method Not Allowed.
- Parameters:
- Returns:
The resulting HTTP response to send to the client.
- Return type:
- class PrePostDispatchViewMixin¶
Bases:
object
Generic view mixin to call methods before/after dispatching the handler.
This is useful for more complex generic views that take advantage of mixins that augment the
dispatch()
method. It allows views to perform additional actions both before and after the HTTP handler is called.This mixin should be placed after any mixins that add decorators to
dispatch()
and before any mixins/classes that inherit fromView()
or provide an implementation ofdispatch()
.Views should generally not have more than one version of this mixin applied.
- dispatch(*args, **kwargs)¶
Dispatch a HTTP request to the right handler.
This will first call
pre_dispatch()
, returning the response if provided by that method. It will then dispatch as normal, and then callpost_dispatch()
.- Parameters:
- Returns:
The resulting HTTP response to send to the client.
- Return type:
- pre_dispatch(*args, **kwargs)¶
Perform actions before dispatching to the HTTP method handler.
This can generate state before calling the appropriate HTTP method handler. It can optionally return a HTTP response, which will be returned directly to the client.
Subclasses don’t need to call the parent method, but can.
- Parameters:
- Returns:
The resulting HTTP response to immediately send to the client. This is optional. Subclasses can return
None
or no value at all to allow the standard HTTP handler to run.- Return type:
- post_dispatch(response, *args, **kwargs)¶
Perform actions after dispatching to the HTTP method handler.
The handler’s response is provided, allowing subclasses to alter the response. It must then return a response (either the existing one or a new one) to send to the client.
Subclasses don’t need to call the parent method, but can.
- Parameters:
response (
django.http.HttpResponse
) – The HTTP response generated by the handler.*args (
tuple
) – Positional arguments that were passed to the handler.**kwargs (
dict
) – Keyword arguments that were passed to the handler.
- Returns:
The resulting HTTP response to send to the client.
- Return type: