Smarter View

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

Bases: View

The foundational base view for all Smarter platform views.

This class serves as the root for every view within the Smarter application, providing a unified interface and shared functionality for web, XML, and text-based views.

All other view classes in the platform are designed to extend this base, inheriting its core logic and conventions. This approach centralizes common behaviors, such as context management, template rendering, HTML minification, and logging, enabling maintainable and scalable development across the platform.

The SmarterView class is intended to be subclassed, allowing developers to build specialized views while leveraging the robust foundation it provides. It encapsulates essential mechanisms for error handling, template processing, and integration with platform-wide configuration, making it the cornerstone of the Smarter view architecture.

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

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.

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

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)[source]

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)[source]

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