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 :-) .
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:
hg clone http://hg.lukeplant.me.uk/python/luke lp_python
Note that this will pull in other code.
"lukeplant_me_uk.django.validator" to your
INSTALLED_APPS setting.<WHERE YOU INSTALLED
IT>/lukeplant_me_uk/django/validator/templates to TEMPLATE_DIRS
setting"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)(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')))
VALIDATOR_APP_VALIDATORS = {
'text/html': '/usr/bin/validate',
'application/xml+xhtml': '/usr/bin/validate',
}
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 ;
}
django-admin.py --settings="yourproject.settings" syncdb
/admin/, do this:
VALIDATOR_APP_IGNORE_PATHS = (
'/admin/',
)
MIT (which means in short that it is open source and free of charge for any use)
maxlength
to max_length, as per change in
Django.© 2005 Luke Plant <L dot Plant dot 98 at
cantab dot net>
Back to lukeplant.me.uk