Problem combining python code and html

Fredrik Lundh fredrik at pythonware.com
Thu May 12 13:45:28 EDT 2005


"Hansan" wrote:

> The change did remove the error message, and I can see what was wrong.
> However there is now a new problem, the only thing that is displayed is one
> single character and that is: <
>
> Do you or anyone else have a suggestion to what the cause to this new
> problem is ?

my fault; I somehow expected you to figure out that you had to fix up
the rest of the function as well, and forgot that you were just cutting
and pasting from a rather lousy example, without understand all the de-
tails.

here's the rest of the relevant code:

    if SubResult[1] == 0:
        raise BadTemplateException

    print "Content-Type: text/html\n\n"
    print SubResult[0]

in the original code, re.subn returns a 2-tuple containing the new string
and a count, but replace only returns the string.  the easiest way to fix
this is to get rid of the BadTemplate check; just replace the above code
with:

    print "Content-Type: text/html\n\n"
    print SubResult

if you really want the BadTemplateException, you can do:

    if SubResult == TemplateInput:
        raise BadTemplateException

    print "Content-Type: text/html\n\n"
    print SubResult

hope this helps!

(I was about to add a short comment on how this illustrates why examples
are not a universal solution to all documentation problems, unless they're
carefully written and carefully documented.  but such talk will only offend
the PHP crowd, so I better skip that)

</F>






More information about the Python-list mailing list