Cleanup: Search object should know if it has an invalid search term
SQL cleanup

Use uppercase, span several lines to improve readability, use
implicit line continuation, invert quoting to avoid escaping.
Cleanup: Invert quoting to avoid escaping
Cleanups: Indents, whitespace and long lines wrap
Cleanup: Remove unnecessary function
Embedded HTML cleanups to improve readability

* Move template context retrieval to the top of the code block,
* Use format() to avoid mixing string with variables,
* Use implicit line continuation instead of endlessly concatenating strings,
* Use print as a function.

This a previous step to strip out the HTML into proper external templates.