API

Convenience Mixins

Use one of the following Mixins in your view to enable the conditional request/response features.

class django_conditional_views.ConditionalGetTemplateViewMixin(**kwargs)[source]

Conditional Request/Response aware mixin for TemplateView

Last-Modified: Calculated from the the template last modified timestamp.

Etag: Calculated from the rendered response.

last_modified_elements = [<class 'django_conditional_views.elements.last_modified.TemplateLastModified'>]
post_render_etag_elements = [<class 'django_conditional_views.elements.etag.RenderedContentEtag'>]
class django_conditional_views.ConditionalGetDetailViewMixin(**kwargs)[source]

Conditional Request/Response aware mixin for DetailView

Last-Modified: Calculated from the latest of the template last modified timestamp and a configurable field on the model object, default ‘modified’.

Etag: Calculated from the rendered response.

last_modified_elements = None
post_render_etag_elements = [<class 'django_conditional_views.elements.etag.RenderedContentEtag'>]
class django_conditional_views.ConditionalGetListViewMixin(**kwargs)[source]

Conditional Request/Response aware mixin for ListView

Last-Modified: Calculated from the latest of the template last modified timestamp and the model objects ‘modified’ field.

Etag: Calculated from the rendered response.

last_modified_elements = None
post_render_etag_elements = [<class 'django_conditional_views.elements.etag.RenderedContentEtag'>]

Base ConditionalGetMixin

class django_conditional_views.ConditionalGetMixin(**kwargs)[source]

Conditional Request/Response aware mixin for View

If a request is made with conditional request headers, such as If-Modified-Since or If-None-Match, the conditional request will be evaluated by django.utils.cache.get_conditional_response.

If the condition of the request is met then then normal view response will be returned, and the Etag and Last-Modified headers will be set if those values could be computed.

If the condition of the request is NOT met then either a 304 Not Modified or a 412 Precondition Failed response will be returned instead.

dispatch(request, *args, **kwargs)[source]

Conditional Request/Response aware wrapper for dispatch.

Calls get_last_modified and get_pre_render_etag to compute the Last-Modified and Etag headers, and then compares those against the conditional request headers (if any) to determine whether to return a 304 or 412 response.

If a 304 or 412 response will be sent the super().dispatch method will never be called, so any computation or side effects done there will not happen.

Otherwise, the response will be rendered and get_post_render_etag will be called to try to get a post_render etag. Once again this will be compared against any conditional request headers to determine whether to send a 304 or 412 response.

If no conditional response will be sent, the Last-Modified and Etag headers for the current response are set and the response is returned.

Return type:HttpResponse
get_last_modified()[source]

Derive a Last-Modified datetime for the view.

Can potentially save a call to dispatch.

Return type:Optional[datetime]
Returns:A datetime representing the last time the view content was modified, or None.
get_post_render_etag(response)[source]

Derive an Etag after the response is rendered.

Return type:Optional[str]
Returns:The calculated ETag or None
get_pre_render_etag()[source]

Derive an ETag before the response is rendered.

Can potentially save a call to dispatch.

Return type:Optional[str]
Returns:The calculated ETag or ‘’
set_response_headers(response, etag=None, last_modified=None)[source]

Sets the Etag and Last-Modified headers on the response

Override if you want to change the final headers.

Parameters:
  • response (SimpleTemplateResponse) – The response to add the headers to.
  • etag (Optional[str]) – The properly formatted etag to add to the header.
  • last_modified (Union[int, float, datetime, None]) – The datetime or timestamp integer to set as the last_modified date.
Return type:

SimpleTemplateResponse

Returns:

The response with the headers added.

Elements

Last-Modified Elements

class django_conditional_views.elements.etag.RenderedContentEtag[source]

Returns the response.content to use for etag hashing.

view_class

alias of django.views.generic.base.TemplateResponseMixin

Etag Elements

class django_conditional_views.elements.last_modified.TemplateLastModified[source]

Returns the last modified time of the template file.

Note that on ListView’s self.object_list = self.get_queryset() must have already been run, see the ConditionalGetListViewMixin for an example implementation.

view_class

alias of django.views.generic.base.TemplateResponseMixin

class django_conditional_views.elements.last_modified.ObjectLastModified(last_modified_field='modified')[source]

Returns the value of a configurable last modified field on the model instance.

view_class

alias of django.views.generic.detail.SingleObjectMixin

class django_conditional_views.elements.last_modified.QuerySetLastModified(last_modified_field='modified')[source]

Returns the latest value of a configurable last modified field on the model queryset.

view_class

alias of django.views.generic.list.MultipleObjectMixin