[mod_python] PSP Templating

Jim Gallacher jpg at jgassociates.ca
Thu Mar 1 12:51:33 EST 2007


John Ruff wrote:
> You are right is was not happening before, my mistake - I went back and 
> checked.  However, I've found that if I change my hello.py to the 
> following, req.content_type doens't have to be defined.  Can you explain 
> this or point me to some reading?  Does this somehow invoke the use of 
> the mod_python.psp handler

No, but the end result is similar. The psp handler sets the content type 
and calls PSP.run().

> 
> def hello(req):
>      s = 'Hello, there!'
>      return psp.PSP(req, filename = 'hello.tmpl', vars = {'greet':s})

Publisher calls the PSP.__str__() method, which looks like this:

     def __str__(self):
         self.req.content_type = 'text/html'
         self.run()
         return ""

which is where the content_type is getting set.

I must admit I am little surprised by the code here... it could be a bit 
of a gotchya for someone using this particular idiom (publishing a PSP 
object) to create something other than html and may represent a bug. 
Perhaps we should be checking to see if the content_type is already set 
rather than blindly overwriting it, so that a user can explicitly set 
the type if they need to?

Digging a little deeper, mod_python.psp.handler() also hard codes the 
content_type as 'text/html'. So it would seem that if one wants to 
generate anything other than 'text/html' one must avoid using the PSP 
handler, or publisher handler as shown in your example above. Not that 
this is pertinent to your current question - just an interesting little 
quirk.

Jim

> ___________________
> 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 11:49 AM, Jim Gallacher wrote:
> 
>> 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