[mod_python] PSP Templating

Jim Gallacher jpg at jgassociates.ca
Thu Mar 1 11:49:26 EST 2007


John Ruff wrote:
> Thanks Olaf.  Is this a new requirement?  It's not mentioned in the PSP 
> 3.3 docs example for templating.  Also, I didn't see this issue when 
> previously using 3.2.10.

Were you using *exactly* the same code? If so I'm surprised it worked 
before.

Some of what you are seeing is the result of the way you are using 
mod_python.publisher, rather than a psp issue. First, don't return 
apache.OK from your hello() function. Publisher looks after the apache 
return code for you internally. Publisher will convert whatever is 
returned by the function to a string and write it to the client. If 
req.content_type is not set publisher will examine whatever is returned 
and make a best effort to guess the correct content_type, and in this 
case guesses wrong. You real content is being written directly by the 
run() method of your PSP instance. In this case you must explicitly set 
the content type. Note that if you were using the mod_python.psp handler 
directly it would have guessed the correct content type and all would 
have well.

As for the extra html being written... I have no idea what is going on 
as I can't reproduce that result using the code snippet you've given. 
For me it just returns:

<html>
<body>
     <h1>Hello, there!</h1>
</body>
</html>
0

Jim


> Thanks.
> ___________________
> John Ruff
> jcruff at gmail.com
> GPG Key: 0x1F691195
> FGPR: 6B50 37C9 10F9 6C4A D381  54B8 319D 7DD9 1F69 1195
> 
> "No one can see past a choice they don't understand." --The Oracle
> 
> 
> On Mar 1, 2007, at 10:13 AM, Olaf Stein wrote:
> 
>> Add
>> req.content_type = 'text/html'
>> Before your tmpl.run(vars = {'greet':s})
>>
>> Olaf
>>
>> On 3/1/07 9:40 AM, "John Ruff" <jcruff at gmail.com> wrote:
>>
>>> I'm trying to understand what I'm doing wrong.  I'm using mod_python
>>> 3.3.1 with apache 2.2.4.  The situation is that while trying to use
>>> PSP as a templating engine, none of the examples work.  Yet normal
>>> 'SetHandler mod_python' with inline html works no problem.
>>>
>>> I have the following:
>>>
>>> <Directory "/home/<user-dir>/Sites/python">
>>>      SetHandler mod_python
>>>      PythonHandler mod_python.publisher
>>>      PythonOption mod_python.psp.cache_database_filename "/tmp/
>>> pspcache.dbm"
>>>      PythonDebug On
>>> </Directory>
>>>
>>> ====hello.py====
>>> from mod_python import apache, psp
>>>
>>> def hello(req):
>>>      s = 'Hello, there!'
>>>      tmpl = psp.PSP(req, filename = 'hello.tmpl')
>>>      tmpl.run(vars = {'greet':s})
>>>      return apache.OK
>>>
>>> ====hello.tmpl====
>>> <html>
>>> <body>
>>>      <h1><%=greet%></h1>
>>> </body>
>>> </html>
>>>
>>>
>>> Still, I'm presented with a webpage that's raw html of what would
>>> have been generated.  When using firefox's Web Developer toolbar to
>>> see the generated source I can see that my html has been surrounded
>>> by an '<html><head></head><body><pre>...</pre></body></html>' block.
>>> So it looks like this:
>>>
>>> [snip]
>>>
>>> <html><head></head><body><pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD
>>> XHTML 1.0 Strict//EN"
>>>      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
>>>
>>> &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
>>> lang="en"&gt;
>>> &lt;head&gt;
>>>      &lt;meta http-equiv="Content-Type" content="text/html;
>>> charset=utf-8"/&gt;
>>>
>>>      &lt;title&gt;hello&lt;/title&gt;
>>>
>>>
>>> &lt;/head&gt;
>>>
>>> &lt;body&gt;
>>>      &lt;h1&gt;Hello, there!&lt;/h1&gt;
>>> &lt;/body&gt;
>>> &lt;/html&gt;
>>> </pre></body></html>
>>>
>>> [/snip]
>>>
>>> Any ideas why this is happening?
>>> ___________________
>>> John Ruff
>>> jcruff at gmail.com
>>> GPG Key: 0x1F691195
>>> FGPR: 6B50 37C9 10F9 6C4A D381  54B8 319D 7DD9 1F69 1195
>>>
>>> "No one can see past a choice they don't understand." --The Oracle
>>>
>>>
>>> _______________________________________________
>>> Mod_python mailing list
>>> Mod_python at modpython.org
>>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>>
>>
>>
>>
>> -------------------------
>> Olaf Stein
>> DBA
>> Center for Quantitative and Computational Biology
>> Columbus Children's Research Institute
>> 700 Children's Drive
>> phone: 1-614-355-5685
>> cell: 1-614-843-0432
>> email: steino at ccri.net
>>
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
> 



More information about the Mod_python mailing list