Pylons (X)HTML Validator Middleware

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 cacheisvalid_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:

Source Download(gzipped):


sudo easy_install W3ValidatorMiddleware-0.1-py2.5.egg



At the top of your import the following

from w3validator import ValidatorMiddleware


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


At the top you need to import cacheisvalid_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'
        # 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.


        <h1>My XHTML Site</h1>
    <div id="footer">

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 Scroll to the bottom of the page to see where I have the validation result.

Written on January 29, 2008