Problem combining python code and html

H

Hansan

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
 
F

Fredrik Lundh

Hansan said:
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)

re.subn takes a regular expression, in which certain characters have
special meanings. for example, in

<!-- *** INSERT JWZ QUOTE HERE *** -->

"*" is reserved character, which means "repeat preceeding expression".

so "***" means repeating a repeated repeat, which makes very little
sense, which is why the RE engine complains.

changing
SubResult = re.subn("<!-- *** INSERT CONTENT HERE *** -->",
Content,TemplateInput)

SubResult = TemplateInput.replace(
"<!-- *** INSERT CONTENT HERE *** -->",
Content
)

should work better.

</F>
 
H

Hansan

Thanks for you reply and Help Fredrik Lundh

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 ?

Thanks for your time
 
B

Bruno Desthuilliers

Hansan a écrit :
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:
(snip a whole lot of code)
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.

Why should it ?
Is the code from dev shed wrong,

Seems like... And it is *very* ugly.
And is there a nother way to combine python code and html templates?

Many. The one I prefer is Zope Page Templates (there's a free-standing
implementation named SimpleTAL), but I didn't tried all and every
existing solutions.
 
H

Hansan

Any suggestions to the problem with only one character being displayed?

Thanks

Thanks for your time
 
F

Fredrik Lundh

Hansan said:
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>
 
H

Hansan

Hi all and thanks for the help, With help from Fredrik I got the code
working, I will also look into the links you guys have suggested.
Hope you all are feeling well and happy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top