This middleware will use the W3 Validator Service to check if your page is valid HTML. It will only check each URI once so that it doesn’t slow down your site by validating every page load. It does this by placing the result of each URI into a memory cache(using Beaker). You can then call cache_is_valid_xhtml() in your template to determine if the current page is valid.

Note: The first time your page loads it will not display if the page is valid or not(It is validated AFTER the first response).

Download Link: http://www.tech9computers.com/W3ValidatorMiddleware-0.1-py2.5.egg

Source Download(gzipped): http://www.tech9computers.com/w3validator.tar.gz

Install

wget http://www.tech9computers.com/W3ValidatorMiddleware-0.1-py2.5.egg
sudo easy_install W3ValidatorMiddleware-0.1-py2.5.egg

Configuration

config/middleware.py

At the top of your middleware.py import the following

from w3validator import ValidatorMiddleware

Then right below CUSTOM MIDDLEWARE HERE

    # CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)
    app = ValidatorMiddleware(app)

lib/helpers.py

At the top you need to import cache_is_valid_xhtml()

from w3validator import cache_is_valid_xhtml

I then created the following method that I call from my templates to return a string saying if the page is valid or not.

def xhtml_str():
    valid = cache_is_valid_xhtml()

    if valid == True:
        return '<em>Valid</em> XHTML 1.0'
    elif valid == False:
        return '<em>NOT</em> Valid XHTML 1.0'
    else:
        # hasn't been validated yet
        return 'XHTML 1.0'

Template Configuration

If you have a base template, you might want to the footer of every page to display if it is valid (X)HTML or not.

Mako

<html>
    <body>
        <h1>My XHTML Site</h1>
    <div id="footer">
        ${h.xhtml_str()}
    </div>
    </body>
</html>

That’s all you need to do to add validation support to your site. You can check the middleware out in action on my site at http://blackberrytracker.com. Scroll to the bottom of the page to see where I have the validation result.