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.

  • context (dict, optional) – The context to use for rendering the template.

Returns:

An HttpResponse with the cleaned HTML content.

Return type:

HttpResponse

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

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

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

Handle GET requests and return a cleaned HttpResponse.

Parameters:

request (HttpRequest) – The HTTP request object.

Returns:

An HttpResponse with the cleaned HTML content.

Return type:

HttpResponse

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.

Parameters:

html (str) – The HTML string to minify.

Returns:

The minified HTML string.

Return type:

str

options(request, *args, **kwargs)

Handle responding to requests for the OPTIONS HTTP verb.

property ready: bool

Returns 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.

remove_comments(html)

Remove HTML comments from an html string.

Parameters:

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

Returns:

The HTML string without comments.

Return type:

str

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, optional) – The context to use for rendering the template.

Returns:

The rendered, cleaned HTML string.

Return type:

str

setup(request, *args, **kwargs)

Initialize attributes shared by all view methods.

template_path: str = ''
view_is_async = False