Changes between Version 2 and Version 3 of ContributedMiddleware


Ignore:
Timestamp:
09/21/2005 06:12:11 AM (10 years ago)
Author:
sune.kirkeby@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ContributedMiddleware

    v2 v3  
    66
    77
    8 I'm tried to add comment to his blog and that fails for some reason, so I'm adding that comment here:
    9 ----
    10 Well 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
    14 class 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 
    39 I'm also added charset part to Content-Type.
    40 ----
     8I'm tried to add comment to his blog and that fails for some reason, so I'm adding that comment here: [ Yup. I broke coments moving to mod_python. D'oh! I've added your comment on the blog, and removed it
     9here. Hope that is ok :) -sune ]
Back to Top