Source

django-output-validator / README.rst

Django output validator

This app validates all the HTML (or potentially other data) that is generated by your Django project. This is meant to be used only in development.

Installation

  • Run setup.py to install the package into your python path.

  • Add "output_validator" to your INSTALLED_APPS setting.

  • If you have removed "django.template.loaders.app_directories.Loader" from your TEMPLATE_LOADERS, you need to add the 'templates' folder to your TEMPLATE_DIRS setting.

  • Insert the middleware "output_validator.middleware.ValidatorMiddleware" near the beginning of the middleware list (which means it will get the the response object after everything else). It must be after every middleware that does post-processing, but mustn't be after GZip, since it can't handle gzipped HTML. ( I just disable the GZip middleware for development)

  • Alter your URL conf to include the URLs for the validator. You need this line inserted somewhere:

    (r'^validator/', include('output_validator.urls'))
    
  • Add a setting to tell the app where to find the 'validate' executable used for validation. This is a dictionary of mimetypes and corresponding validators, allowing this app to be extended to any other generated content:

    OUTPUT_VALIDATOR_VALIDATORS = {
      'text/html': '/usr/bin/validate',
      'application/xml+xhtml': '/usr/bin/validate',
    }
    

    I usually use a small wrapper for this executable that pops up a message when it fails - the following works for GNOME (if you have the notify-send program installed):

    #!/bin/sh
    validate "$1" || {
        notify-send "Validation failed";
    }
    
  • Finally, run the django admin script to set up the database tables:

    ./manage.py --settings="yourproject.settings" syncdb
    

    OR, if you are using South:

    ./manage.py --settings="yourproject.settings" migrate output_validator
    
  • Optionally, set the following settings:

    • OUTPUT_VALIDATOR_IGNORE_PATHS - this is a list of path prefixes that will be ignored. For example, if you have the admin at /admin/ you can ignore any errors in the admin with this:

      OUTPUT_VALIDATOR_IGNORE_PATHS = [
          '/admin/',
      ]
      

Usage

When browsing any of your pages in development, all HTML will be validated. If it fails, it will be logged. You can see all failures at 'http://localhost:8000/validator/' (assuming local development and the URL conf suggested above). Use the app to delete old failures once they have been fixed.