SmarterWebHtmlView

class smarter.lib.django.views.SmarterWebHtmlView(**kwargs)[source]

Bases: SmarterView

Base view for smarter web views.

Includes helpers for rendering, minifying and stripping out developer comments.

__init__(**kwargs)

Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.

classmethod as_view(**initkwargs)

Main entry point for a request-response process.

clean_http_response(request, template_path, *args, context=None, **kwargs)[source]

Render a template and return an HttpResponse with comments removed.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • template_path (str) – The path to the template to render.

  • args – Ignored.

  • context (dict[str, Any] | None) – The context to use for rendering the template.

  • kwargs – Ignored.

Return type:

HttpResponse

Returns:

An HttpResponse with the cleaned HTML content, or the error response of render_clean_html() if the template could not be rendered.

context: dict[str, Any] = {}
dispatch(request, *args, **kwargs)
property formatted_class_name: str

Return the class name in a formatted string along with the name of this view.

Returns:

The dotted name of this view class and its instance id, formatted for logs.

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

Handle GET requests and return a cleaned HttpResponse.

A view without a template, e.g. a JSON api that only answers POST, has nothing to render, so a GET receives 405 Method Not Allowed instead of a 500 (issue #963).

Parameters:
Return type:

HttpResponse

Returns:

An HttpResponse with the cleaned HTML content, or a 405 response when the view has no template.

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

Answer a GET to a view that has no template with 405 Method Not Allowed.

Django’s own http_method_not_allowed() never runs for these views, because this class defines get(). Its Allow header would list GET and HEAD, so they are removed.

Parameters:
Return type:

HttpResponse

Returns:

A 405 response whose Allow header lists the methods that the view handles.

http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
http_method_not_allowed(request, *args, **kwargs)
minify_html(html)

Minify an html string, by removing the whitespace between tags.

It is one regular expression, not a parse: HTMLMinifyMiddleware parses every html response anyway.

Parameters:

html (str) – The HTML string to minify.

Return type:

str

Returns:

The minified HTML string.

options(request, *args, **kwargs)

Handle responding to requests for the OPTIONS HTTP verb.

property ready: bool

Return True if the view is ready to handle requests, False otherwise.

This can be used to check if the necessary context or data is available before processing a request.

Returns:

True if the view is ready.

remove_comments(html)

Remove HTML comments from an html string.

Parameters:

html (str) – The HTML string from which to remove comments.

Return type:

str

Returns:

The HTML string without comments.

render_clean_html(request, template_path, context=None)

Render a template as a string, with comments removed and minified.

Parameters:
  • request (HttpRequest) – The HTTP request object.

  • template_path (str) – The path to the template to render.

  • context (dict[str, Any] | None) – The context to use for rendering the template, or the view’s context if None or empty.

Return type:

str | HttpResponse

Returns:

The rendered, cleaned HTML string, or a 500 response if the template could not be rendered.

setup(request, *args, **kwargs)

Initialize attributes shared by all view methods.

template_path: str = ''
view_is_async = False