Validator App for Django

Summary

This simple app does validation on all the HTML generated by your Django app, and keeps track of any pages that fail. You will no longer have any excuse for pages that aren't valid HTML :-) .

What it does

It uses a middleware to intercept all outgoing HTML and validate it using an external command line application. It then logs any failures, and provides some pages for viewing and clearing out the list of failures. This means that it can catch HTML errors that are only produced by 'POST' actions, which otherwise are painful to check for (since a crawler won't get them).

It currently does this synchronously, so it slows your app down. On my machine, it's only about 1/4 second per page, so it's well worth it, and obviously it is only meant for development purposes - you just comment out a few config lines to disable it for live sites.

Here's a screenshot of it picking fault in an early version of itself:

Django validator app displaying details of a page with invalid
HTML

Requirements

Download

Installation and usage

  1. Extract the package and use 'python setup.py install' to install. (Alternatively just copy the lukeplant_me_uk directory to somewhere in your python path).
  2. Add "lukeplant_me_uk.django.validator" to your INSTALLED_APPS setting.
  3. If you have removed 'app_directories.load_template_source' from your TEMPLATE_LOADERS, you will have to add the folder <WHERE YOU INSTALLED IT>/lukeplant_me_uk/django/validator/templates to TEMPLATE_DIRS setting
  4. Insert the middleware "lukeplant_me_uk.django.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)
  5. Alter your URL conf to include the URLs for the validator. You need this line inserted somewhere:
    (r'^validator/', include('lukeplant_me_uk.django.validator.urls'))

    I usually do this conditionally on the settings.DEBUG settings:

    if settings.DEBUG:
        urlpatterns = urlpatterns + patterns('',
            (r'^validator/', include('lukeplant_me_uk.django.validator.urls')))
    
  6. 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:
    VALIDATOR_APP_VALIDATORS = {
        'text/html': '/usr/bin/validate',
        'application/xml+xhtml': '/usr/bin/validate',
    }
    

    If executables other than Liam Quinn's 'validate' are used, then ensure they work in the same way:

    The exit code is currently ignored, but 'validate' sets it correctly so it's very easy to wrap it in a small shell script that does other things upon failure like beep or display something on the screen. Like this one (the second part requires KDE and assumes you are doing django-admin.py runserver from a KDE session):

    #!/bin/sh
    	
    validate $1 || 
    { 
    	beep -l 5; 
    	dcop knotify default notify "validation failed" "django validator" "invalid page found" "" "" 16 0 ;
    }
    
  7. Run the django admin script to set up the database tables:
    django-admin.py --settings="yourproject.settings" syncdb
  8. You can set an optional setting 'VALIDATOR_APP_IGNORE_PATHS' in your settings.py module. This should be a sequence of strings which are ignored by the middleware if they appear at the beginning of the request path. For example, to exclude all paths beginning with /admin/, do this:
    VALIDATOR_APP_IGNORE_PATHS = (
        '/admin/',
    )
    

Notes

Bugs

LICENSE

MIT (which means in short that it is open source and free of charge for any use)

Changelog

Version 1.4 - 2008-04-28

Version 1.3 - 2007-11-05

Version 1.2 - 2007-04-18

Version 1.1 - 2005-12-14

Version 1.0 - 2005-11-19

About

© 2005 Luke Plant <L dot Plant dot 98 at cantab dot net>
Back to lukeplant.me.uk