[mod_python] My template script isn't working

Graham Dumpleton grahamd at dscpl.com.au
Sun Aug 28 20:28:34 EDT 2005


Where ever you keep your templates, you can't use a relative path to the
filename argument as the current working directory could be set to
anything. You therefore must use an absolute pathname meaning you either
have to hard code pathnames (bad), express them relative to a hard coded
pathname set as a PythonOption setting (not so bad), or express them
relative to the location of the script as determined from __file__.

Thus, even if that exact code you provided may work now, there is no
gaurantee that it would work under a different Apache setup where the
working directory of Apache is different.

Anton wrote ..
> Actualy I keep *.tmpl in "templates" directory.
> I turned apache debug logging on and got 1 'info':
> [Mon Aug 29 02:09:58 2005] [info] (104)Connection reset by peer:
> core_output_filter: writing data to the network
> 
> It explains my problem. But I don't know how it helps to solve my problem.
> 
> 2005/8/29, Graham Dumpleton <grahamd at dscpl.com.au>:
> > 
> > On 29/08/2005, at 8:49 AM, Anton wrote:
> > 
> > > I am migrating from php to python. Simple script are attached with
> > > templates.
> > >
> > > The problem is script isn't being processed by apache (It's not
> > > AddHandler stuff). And I think something is segfaulting.
> > >
> > > What is wrong with it?
> > >
> > > Script works if I, for example, change QUOTE to QUOTE2 and error
> > > occurs:
> > > NameError: name 'QUOTE' is not defined
> > > <index.py><style.css><header.tmpl><index.tmpl><right.tmpl>_____________
> > > _________________________________
> > 
> > In your code you have:
> > 
> >    from mod_python import apache,psp
> > 
> >    def index(req):
> >      quote = 'quote_of_the_day'
> >      tmpl = psp.PSP(req, filename='templates/index.tmpl')
> >      tmpl.run( vars={'QUOTES':quote} )
> >      return apache.OK
> > 
> > The current working directory will not be that of your script. For
> > starters, try instead:
> > 
> >    from mod_python import apache,psp
> > 
> >    import os
> > 
> >    __here__ = os.path.dirname(__file__)
> > 
> >    def index(req):
> >      quote = 'quote_of_the_day'
> >      tmpl = psp.PSP(req, filename=os.path.join(__here__,'index.tmpl'))
> >      tmpl.run( vars={'QUOTES':quote} )
> >      return apache.OK
> > 
> >


More information about the Mod_python mailing list