[mod_python] PSP TypeError: cannot concatenate 'str' and 'instance' objects

Graham Dumpleton grahamd at dscpl.com.au
Fri Feb 17 04:52:42 EST 2006


The PSP class always writes it output direct to the request object  
and it
is not available as a string without doing some trickery. Even if using
publisher, you can do something like:

     req.content_type            = 'text/html'
     gallery_tmpl                = psp.PSP(req, filename='gallery.tmpl')
     req.write("some HTML prologue")
     gallery_tmpl.run(vars       = { 'title': title, 'caption':  
caption, 'body': body  })
     req.write("some HTML epilogue")

Ie., don't try and concatenate anything as strings, just write the  
bits out
in sequence.

If you really need to capture the PSP output in a string have a read of:

   http://www.modpython.org/pipermail/mod_python/2005-June/018231.html

Graham

On 17/02/2006, at 8:39 PM, Corey McGuire wrote:

> Hello all,
>
> I am learning python and PSP at the same time, which may be a bit of a
> mistake, but I think I am doing well.
>
> I am working on moving my PHP image gallery:
> http://www.coreyfro.com/~coreyfro/gallery.php?gallery=b-man-2003
>
> To python:
> http://127.0.0.1/~coreyfro/gallery/gallery
>
> This is really just an exercise, but it's kinda my own version of  
> "Hello
> World"
>
> I am getting hung up ok this block of code:
>
> cell_tmpl = psp.PSP(req, filename='cell.tmpl', vars = {'gallery':  
> gallery,
> 'i': i, 'path': path, 'file': dirList[i], 'caption': caption})
> body = body + cell_tmpl
>
> Now, the kicker is "body = body + cell_tmpl"... If I leave it like  
> that, I
> get the following
> http://127.0.0.1/~coreyfro/frozen/gallery1/gallery/?gallery=b-man-2003
>
> If I try converting it to a string (like "body = body + str 
> (cell_tmpl)")
> something TOTALY wacky happens
> http://127.0.0.1/~coreyfro/frozen/gallery2/gallery/?gallery=b-man-2003
>
> It's outputting the HTML I am trying to append to body and NOT
> concatinating it with body.
>
> How do I put two and two together?
>
> Here's the code in full
>
> from mod_python import psp
> import os
>
> def report(req, title, caption, body):
>     req.content_type            = 'text/html'
>     gallery_tmpl                = psp.PSP(req,  
> filename='gallery.tmpl')
>     gallery_tmpl.run(vars       = { 'title': title, 'caption':  
> caption,
> 'body': body  })
>
> def index(req):
>     title                       = "Coreyfro's Handy, Dandy, Super- 
> duper,
> Gallery Engine!"
>     caption                     = "It's free, it's fast, it's  
> incomplete!"
>     body                        = "You are probably here by  
> mistake<br>\
>                                    Please feel free to be  
> confused<br>\
>                                    I know I am"
>     report(req, title, caption, body)
>
> def gallery(req, gallery='', columns=''):
>     if columns:
>         columns                 = int(columns)
>     else:
>         columns                 = 5
>
>     if gallery:
>         i                       = 0
>         column                  = 1
>         title                   = gallery
>         caption                 = "Click on an image below to see it's
> full sized version"
>         path                    = "/home/coreyfro/public_html/gallery"
>         dirList                 = os.listdir(path+"/"+gallery+"/ds")
>         body                    = " " * 6 + "<table>\n" + " " * 8 +  
> "<tr>\n"
>
>
>         while i < len(dirList):
>             if column < columns:
>                 column          = column + 1
>             else:
>                 column          = 1
>                 body            = body + " " * 8 + "</tr>\n" + " "  
> * 8 +
> "<tr>\n"
>
>             cell_tmpl           = psp.PSP(req,  
> filename='cell.tmpl', vars
> = {'gallery': gallery, 'i': i, 'path': path, 'file':
> dirList[i], 'caption': caption})
>             body                = body + cell_tmpl
>             i                   = i + 1
>
>
>         body                    = body + " " * 8 + "</tr>\n" + " "  
> * 6 +
> "</table>"
>         report(req, title, caption, body)
>
>     else:
>         index(req)
>
> def view(req, gallery, index):
>     pass
>
> _______________________________________________
> 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