Code

Changes between Version 1 and Version 2 of ContributedMiddleware


Ignore:
Timestamp:
09/21/05 02:02:37 (9 years ago)
Author:
nesh <nesh [at] studioquattro [dot] co [dot] yu>
Comment:

I can't post comment to his blog, so I write comment here

Legend:

Unmodified
Added
Removed
Modified
  • ContributedMiddleware

    v1 v2  
    44 
    55Sends application/xhtml+xml header to browsers that understand it. 
     6 
     7 
     8I'm tried to add comment to his blog and that fails for some reason, so I'm adding that comment here: 
     9---- 
     10Well you reversed parameters in replace ;), also I think that using re for checking Accept header is a bit of a overkill, here is my solution: 
     11 
     12{{{ 
     13#!python 
     14class XHTMLAsHTMLMiddleware: 
     15    """I change content-type application/xhtml+xml into text/html, if the 
     16    browser does not support the XHTML content-type.""" 
     17 
     18    def process_response(self, request, response): 
     19        accept = request.META.get('HTTP_ACCEPT', '') 
     20        user_agent = request.META.get('HTTP_USER_AGENT', '') 
     21        if ('application/xhtml+xml' in accept) or ('W3C_Validator' in user_agent): 
     22            ct = response['Content-Type'] 
     23            ct = ct.replace('text/html', 'application/xhtml+xml') 
     24            response['Content-Type'] = ct 
     25        if ';' not in response['Content-Type']: 
     26            # add default encoding 
     27            ct = response['Content-Type'] 
     28            try: 
     29                from django.conf.settings import DEFAULT_ENCODING 
     30            except ImportError: 
     31                DEFAULT_ENCODING = 'utf-8' 
     32            ct = ct + '; charset=' + DEFAULT_ENCODING 
     33            response['Content-Type'] = ct 
     34        return response 
     35    # process_response 
     36# XHTMLAsHTMLMiddleware 
     37}}} 
     38 
     39I'm also added charset part to Content-Type. 
     40----