Django middleware to validate HTML and XML responses
Project description
Django HTML and XML Validator
Django_html_xml_validator
is a Django middleware to validate HTML and XML
responses generated by your application. This includes but is not limited to
Django views using render()
and Django HTML templates.
Features:
- Specific error locations in case validation finds issues.
- Runs locally without the need to upload your page to an external validation service.
- Uses only Python packages without the need to install external tools from other ecosystems.
- Fast because based on lxml and its native components.
This makes it feasible to perform validation while running your test suite.
Installation
To install, depending on your package manager, run:
pip install --update django_html_xml_validator
or
poetry add django_html_xml_validator
Usage
To add validation to your project, add it to settings.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+xml
- text/html
- XML:
- application/xml
- text/xml
In 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).
Configuration
By default, validation is active when the Django DEBUG
mode is enabled in
settings.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 to settings.py
:
VALIDATE_HTML = True
VALIDATE_XML = True
If 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 templates
Disabling validation for specific tests
In case validation is not useful for selected tests (for example when processing deliberately huge documents), it can be disabled with the override_settings annotation. For example:
from django.test import override_settings
@override_settings(VALIDATE_XML=False)
def test_can_build_huge_xml():
...
Limitations
- Validation 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.
License
Copyright (c) 2022 ITELL.SOLUTIONS GmbH, Graz, Austria.
Distributed under the
MIT license. For details refer to
the file LICENSE
.
The source code is available from https://github.com/itell-solutions/django_html_xml_validator.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_html_xml_validator-1.0.0.tar.gz
.
File metadata
- Download URL: django_html_xml_validator-1.0.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3be83a56bbdae39cbeb63265811641e3ee2e725529decd24ace28bf75a7e7c9 |
|
MD5 | 27e35c572a30598c740010aec0393ed0 |
|
BLAKE2b-256 | 59e7eda96ebe6b649be8c1d486de0db5e4ddb4d7f69ab8f3b7b655b962082ec3 |
File details
Details for the file django_html_xml_validator-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: django_html_xml_validator-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 164ca0197464fb19be13fc8206f706042d5221daa22561d1f2764509192908f4 |
|
MD5 | 18db74942ee8244ed0e7dcb5e25ddc9c |
|
BLAKE2b-256 | 6ac9425afebcef720f6583a268a949ba8dbceb32e282889720962191ba7c2875 |