Validating Python - need doctype HTML strict

P

PapaRandy

Hello,

I am trying to validate the following .py webpage as HTML (through
W3C).

I put:

-----------------------------------------------------------------------------

print "Content-type: text/html; charset=utf-8"

import time

print

print """<html><head>

<title>Current Time</title>

</head> <body>

<h1>Current Time</h1>"""

print "<p>Right now, it is "

print "<strong>", time.asctime(), "</strong></p>"

print "</body></html>"

---------------------------------------------------

However, the .py page should also be valid (X)HTML - it needs a
doctype...

When I add the doctype, and other necessities to the .py page

(i.e., <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"" xml:lang="en" lang="en">)

I get an invalid script error.

Can anyone help me to get the XHTML doctype to validate and execute on
Python web page?

Thank you!
 
B

Ben Sizer

PapaRandy said:
When I add the doctype, and other necessities to the .py page

(i.e., <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"" xml:lang="en" lang="en">)

I get an invalid script error.

Bear in mind, that this isn't 'a webpage', it's a computer program that
creates a webpage. So you're not 'adding a doctype', you're 'adding
some code to output a doctype', and similarly it's not Python you're
validating, it's the output it creates that you validate. These are
important distinctions.

Anyway, what is the exact line of code you use to 'add the doctype'?
And what is this 'invalid script error'? It's hard to debug your code
when we have to guess what it is! However, because I'm in a good mood,
I'll have a go.

You probably need to escape the double quotes in the doctype because
they unintentionally correspond with the double quotes in your print
statement. The print statement uses double quotes to delimit the
output, and the doctype uses them to delimit the type. Unfortunately
the print statement probably interprets the start of the doctype's type
field as the end of the print statement. Add a backslash before each
double quote within your doctype and see how that goes. Alternatively
you could possibly use single quotes in it instead.
 
C

Circa

Thanks Ben,

As you can see it's my first try at Python...or any programming for
that matter.

I solved the problem of validation by adding three """ quotes after the
print command and before the body, as follows:

Thanks for clearing that up!

-----------------------

import time

#print HTTP/HTML header stuff
print """Content-type: text/html; charset=utf-8\n\n

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Current Time</title>

</head> <body>
"""

print

#print HTML body using Python-HTML hybrid script
print "<h1>Current Time</h1>"

print "<p>Right now, it is "

print "<strong>", time.asctime(), "</strong></p>"

print """<p>
<a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>
</p>
"""
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top