[mod_python] combine psp with publisher woes

Graham Dumpleton grahamd at dscpl.com.au
Thu Aug 11 18:21:03 EDT 2005


On 12/08/2005, at 7:39 AM, Graham Dumpleton wrote:

>
> On 11/08/2005, at 11:29 PM, km wrote:
>
>>
>> Hi all,
>> i have tried  the classic example which separates template from logic.
>> i am using mod_python 3.14 with apache 2.0.54
>>
>> # template ->  hello.template
>> <html>
>> <h1><%= greet %></h2>
>> </html>
>>
>> # script hello2.py
>> from mod_python import apache, psp
>> def handler(req):
>>     tmpl = psp.PSP(req, filename='./hello.template')
>>     tmpl.run(vars = {'greet': 'world'})
>>     return  apache.OK
>> i have placed both hello2.py and hello.template in  
>> /usr/local/apache2/htdocs/test1 dir.
>
> The above is not quite written in style required by publisher.
> A publisher function to be able to automatically determine
> content type needs content to be the return value of the function.
> Here the PSP object writes directly to the req object. Thus use:
>
> from mod_python import apache, psp
> def handler(req):
>     req.content_type = 'text/html'
>     tmpl = psp.PSP(req, filename='./hello.template')
>     tmpl.run(vars = {'greet': 'world'})
>     return " "

BTW, for determining name of template you should use something
like:

   import os
   template = os.path.splitext(__file__)[0] + '.template'

   from mod_python import apache, psp
   def handler(req):
       req.content_type = 'text/html'
       tmpl = psp.PSP(req, filename=template)
       tmpl.run(vars = {'greet': 'world'})
       return " "

Accept that you may have put "./hello.template" for illustrative
purposes only, but thought I would mention this anyway so no one
else is confused when looking at this in the mailing list archive.

:-)

Graham



More information about the Mod_python mailing list