|
Robert Fendt
rmfendt at web.de
Sat Sep 25 18:33:11 EDT 2004
Hi,
I am playing around with mod_python at the moment (partly because I
developed a bit of a dislike for PHP lately). Normally I use XHTML and use
the "Accept:" header information by the UA to determine if it can parse
the document as XML. In that case, it gets the document as
application/xhtml+xml, otherwise (e.g., the IE) as text/html. The code I
use for this in PHP is irrelevant, of course. In mod_python currently I
use this:
-----------------------------
CHARSET = "utf-8"
def index(req):
xmlctype = 'application/xhtml+xml'
htmlctype = 'text/html'
supportsxml = string.count(req.headers_in.get('Accept', ""), xmlctype)
if (supportsxml):
ctype = xmlctype
else:
ctype = htmlctype
req.content_type = ctype + '; charset=%s' % CHARSET
-----------------------------
This works quite well, but problems start when I try to include a psp page
template like this:
-----------------------------
vars = {'usexml': supportsxml,
'charset': CHARSET}
page = psp.PSP(req, 'index.psp', vars=vars)
return page
-----------------------------
Whatever I do, the page is _always_ delivered as "text/html". If I repeat
the content_type assignment inside the psp template, it works, but of
course I do not want to repeat the recognition code in every page
template. A look inside psp.py showed that indeed the content_type is
overwritten. Here:
-----------------------------
def __str__(self):
self.req.content_type = 'text/html'
self.run()
return ""
-----------------------------
and here:
-----------------------------
def handler(req):
req.content_type = "text/html"
-----------------------------
I would suggest changing it to what the publisher handler does (if this
does not break anything, but at least it seems to work for me), e.g.:
-----------------------------
def __str__(self):
if not self.req._content_type_set:
self.req.content_type = 'text/html'
self.run()
return ""
-----------------------------
One final remark: defining __str__(self) like above is (in my opinion) a
very ugly hack. On trying something like
page=str(psp.PSP(req, 'page.psp'))
I would expect "page" to contain the PSP rendering of 'page.psp' as a
string. What I would not expect, however, is the (attempted) string
conversion causing a direct write to the UA. In fact I tried this while
trying to get the psp class not to rewrite my content-type and was quite
surprised at the result.
Regards,
Robert
|