Package: django-html-xml-validator
Django HTML and XML ValidatorDjango_html_xml_validatoris a Django middleware to validate HTML and XML
responses generated by your application. This includes but is not limited to
Django views usingrender()and Django HTML templates.Features:Specific error locationsin case validation finds issues.Runs locallywithout the need to upload your page to an external
validation service.Uses only Python packageswithout the need to install external tools
from other ecosystems.Fastbecause based onlxmland its native components.This makes it feasible to perform validation while running your test suite.InstallationTo install, depending on your package manager, run:pipinstall--updatedjango_html_xml_validatororpoetryadddjango_html_xml_validatorUsageTo add validation to your project, add it tosettings.MIDDLEWARE.MIDDLEWARE=[...,"django_html_xml_validator.middleware.HtmlXmlValidatorMiddleware",]In most cases you only want it to validate the HTML generated by your views
directly, so it would be the last entry. Especially if you have other
middleware installed that modifies your HTML like adding the Django Debug
toolbar or minifying it.For example:MIDDLEWARE=[# Possible middleware your project requires....,# Example middleware that modifies the HTML."django_minify_html.middleware.MinifyHtmlMiddleware","debug_toolbar.middleware.DebugToolbarMiddleware",...,# Put validation middleware toward the end to ensure only your HTML/XML is validated."django_html_xml_validator.middleware.HtmlXmlValidatorMiddleware",]After that, responses with a matching content type are validated:HTML:application/xhtml+xmltext/htmlXML:application/xmltext/xmlIn case the response is valid, the middleware returns the original response
and HTTP status code verbatim.In case errors have been found, the response includes an HTML page detailing
the errors with an HTTP status code of 500 (internal server error).ConfigurationBy default, validation is active when the DjangoDEBUGmode is enabled insettings.py. In a reasonably configured project this means during local
development and while running the test suite, but not once deployed to a
server.For more granular control, add the following tosettings.py:VALIDATE_HTML=TrueVALIDATE_XML=TrueIf you are sure all your HTML pages are actually XHTML (which sadly will not
be the case as soon as your code contains forms based on standard Django
forms), you can enforce HTML to be validated as XHTML:VALIDATE_HTML_AS_XHTML=True# WARNING: Will fail with standard form templatesDisabling validation for specific testsIn case validation is not useful for selected tests (for example when
processing deliberately huge documents), it can be disabled with theoverride_settingsannotation. For example:fromdjango.testimportoverride_settings@override_settings(VALIDATE_XML=False)deftest_can_build_huge_xml():...LimitationsValidation does not apply to stream responses.Validation of HTML5 uses a hack to ignore errors about invalid tags on
sectioning elements like<nav>or<article>.Validation of XML only checks if the document is well-formed but does not
validate against a schema or DTD. Technically lxml could do all this but
would require more setup. If you need such a feature, feel free to submit a
pull request.LicenseCopyright (c) 2022 ITELL.SOLUTIONS GmbH, Graz, Austria.Distributed under theMIT license. For details refer to
the fileLICENSE.The source code is available fromhttps://github.com/itell-solutions/django_html_xml_validator.