[mod_python] PSP content-type problem

Martin Slouf xslom03 at vse.cz
Sun Sep 26 10:59:57 EDT 2004


On Sat, Sep 25, 2004 at 10:51:45PM +0200, Kamil Niechajewicz wrote:
> On Sat, 25 Sep 2004 17:33:11 +0200, Robert Fendt <rmfendt at web.de> wrote:
> 
> >I would suggest changing it to what the publisher handler does (if this
> >does not break anything, but at least it seems to work for me), e.g.:
> 
> yeah, i agree with you. this should be changed.
> i have different matter that concerns me. actually psp instantly
> does req.write while processing the template, so its possible that
> only part of the page will be displayed due some processing errors
> (like using not supported by 'vars = {}' key name etc.) when it
> occurs we get an ugly error inside our page that was partially
> displayed to the user. that is not a very pretty solution.

well, i agree that buffer would be nice, but you can do all the nescessary
error checking in handler and than just use vars to pass the html strings
generated

there is also possible to pregenerate the page and store it whole in a string

def registration(req, err = None):
    err_tmpl = get_err_tmpl(req, err)
    args = {"title": _("Registration"),
            "data" : get_reg_data(req),
            "err_tmpl" : err_tmpl}
    tmpl = TEMPLATES_DIR + "registration.html"
    return psp.PSP(req, tmpl, vars = args)

def get_err_tmpl(req, reg_err):
    tmpl = ""
    if (reg_err == None): pass
    else: tmpl = psp.PSP(req, TEMPLATES_DIR + "error.tmpl",
                         vars = {"err": reg_err})
    return tmpl

the 1st func is handler, err_tmpl contains any errors that are reported to user
and I use in the 2nd func

tmpl = psp.PSP(req, TEMPLATES_DIR + "error.tmpl",
               vars = {"err": reg_err})

'tmpl' is not an standalone page -- just a part of it and this code will
return its string representation (a layer actually)

i guess its pretty clear how you can use try...except around the code and catch
the exceptions, in fact, i use special handler that checks the data for me and
if ok, calls registration(reg) and if not it calls registration(reg, err) as
the last thing in its processing

m.


More information about the Mod_python mailing list