Problem combining python code and html

Hansan none
Wed May 11 11:56:56 EDT 2005


Hi all, I hope you have time to help me out a little. My problem is that I 
want to combine some python code I have made with some html templates, I 
found a tutorial at dev shed: 
http://devshed.spunge.org/Server_Side/Python/CGI/page6.html
and : http://devshed.spunge.org/Server_Side/Python/CGI/page5.html and tried 
to do it like they do, but it doesn't work.

I used the same code they use for making the two display functions and the 
same template for the html form. I even tried using the form template they 
use too, but it still dosnt work. I will now show how the functions and 
templates look like.

The two display functions are:
def Display(Content):
    TemplateHandle = open(TemplateFile, "r")
    TemplateInput = TemplateHandle.read()
    TemplateHandle.close()
    BadTemplateException = "There was a problem with the HTML template."
    SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->", 
Content,TemplateInput)
    if SubResult[1] == 0:
        raise BadTemplateException

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

def DisplayForm():
    FormHandle = open(FormFile, "r")
    FormInput = FormHandle.read()
    FormHandle.close()
    Display(FormInput)

The html template form is:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
  <head>
    <META NAME="something" CONTENT="Somethingelse">
    <title>Title</title>
  </head>
  <body>
                <!-- *** INSERT CONTENT HERE *** -->
  </body>
</html>

and the form template is:
<FORM METHOD="POST" ACTION="name.py">
<p>Signin number:<br> <INPUT type="text" NAME="playerid">
<p>Code:<br> <INPUT type="password" NAME="password"></p>
<input type=hidden name="action" value= tjeck >
<p><input type=submit value='Send'></p></form>

A very simple code that should tjeck if the code works could be

#!/pack/python-2.3.2/bin/python2.3
import re
import cgi

FormFile = "forma.html"
TemplateFile = "template.html"

def Display(Content):
    TemplateHandle = open(TemplateFile, "r")
    TemplateInput = TemplateHandle.read()
    TemplateHandle.close()
    BadTemplateException = "There was a problem with the HTML template."
    SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->", 
Content,TemplateInput)
    if SubResult[1] == 0:
        raise BadTemplateException

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


def DisplayForm():
    FormHandle = open(FormFile, "r")
    FormInput = FormHandle.read()
    FormHandle.close()
    Display(FormInput)


if not form.has_key('action'):
    DisplayForm()
else:
    print "I had a key"

The meaning of this little code is, that the first time the script is run, 
the form has to be displayed, and when the submit button is pressed the 
script reloades, now the script has the key "action" and something else is 
being done. But I get the error message:
error: multiple repeat
      args = ('multiple repeat',) ", Content,TemplateInput) File 
"/home//Python-2.3.2//lib/python2.3/sre.py", line 151, in subn return 
_compile(pattern, 0).subn(repl, string, count)
I tried making the templates global, but that dosnt help either.
Is the code from dev shed wrong, or am I doing something wrong.
And is there a nother way to combine python code and html templates?
Thanks for your time 





More information about the Python-list mailing list