Thursday, 10 April 2008
Django XHTML-Mortifier
Last week I decided to set the content-type of my xhtml-pages to application/xhtml+xml. This of course caused some problems for IE as it doesn't accept this content-type. I found some Django middleware snippet which can do this convertion, however it was doing too much for me. So I rewrote it to fulfil my needs.
Also my uber Django-blog supports reStructuredText markup now (in addition to Textile) and along with that I implemented syntax highlighting using Pygments.
Anyway, here's the middleware source (using the cool Pygments highlighter):
import re
CONTENT_TYPE_RE = re.compile(r'="application\/xhtml\+xml')
HTML_RE = re.compile(r'html', re.I)
class XhtmlMortifierMiddleware(object):
"""
This middleware converts responses set with the application/xhtml+xml
content-type to text/html if the user-agent does not accept XHTML responses.
Other kind of responses are being ignored.
Make sure the XhtmlMortifierMiddleware appears after the GZipMiddleware in
the MIDDLEWARE_CLASSES list in settings.py.
"""
def _accepts_xhtml(self, request):
if "/xhtml+xml" in request.META.get("HTTP_ACCEPT", "").lower():
return True
else:
return False
def process_response(self, request, response):
if not HTML_RE.search(response["Content-Type"]):
return response
if self._accepts_xhtml(request) and not \
response["Content-Type"].split(";")[0] == "text/html":
response["Content-Type"] = "application/xhtml+xml; charset=utf-8"
else:
response["Content-Type"] = "text/html; charset=utf-8"
response.content = CONTENT_TYPE_RE.sub('="text/html', \
response.content, 1)
response['Content-Length'] = str(int(response['Content-Length']) - 12)
return response
I listed it on Djangosnippets
Ugh
said Saturday, 12 July 2008:Seriously, why?? Are you using MathML or something? read http://www.tangerinesmash.com/writings/2007/jun/11/html-vs-xhtml-best-tool-job/
Jeffrey Gelens
said Saturday, 12 July 2008:Everyone’s own taste?
(markup will be applied later)
Feeds
Latest Comments
Articles
Links
Blogroll
Gelens.org is powered by the latest Newforms-Admin branch of Django, running on Python 2.5.2 and served by Apache 2.2.9 with mod_wsgi 2.3, proxied by Nginx 0.6.32. The database is PostgreSQL 8.3.3. All of this is running on the latest Arch Linux on a 360MB Linode Xen VPS.
by Tom Asaro
by Wezz6400
by Jeffrey Gelens
by Ian Lewis
by Jeffrey Gelens