[CGI] Why is HTML not rendered?

G

Gilles

Hello

I'm learning how to call Python scripts through the different
solutions available.

For some reason, this CGI script that I found on Google displays the
contents of the variable but the HTML surrounding it is displayed
as-is by the browser instead of being rendered:

--------------
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

import cgi
form = cgi.FieldStorage()

# get a value from the form
value = form.getvalue("dummy")

print "Content-Type: text/plain;charset=utf-8"
print

# print a document
print "<P>You typed: <TT>%s</TT></P>" % (
cgi.escape(value),
)
 
A

Alexander Blinne

For some reason, this CGI script that I found on Google displays the
contents of the variable but the HTML surrounding it is displayed
as-is by the browser instead of being rendered:
print "Content-Type: text/plain;charset=utf-8"

With this line you tell the browser to expect a simple plain text file
and no html. Change the line to

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

and it should work.
 
D

Dan Sommers

For some reason, this CGI script that I found on Google displays the
contents of the variable but the HTML surrounding it is displayed
as-is by the browser instead of being rendered:

.... [with all due respect and apologies to another thread on this list!]
print "Content-Type: text/plain;charset=utf-8"

That line tells the browser that the response is plain text.

Do this instead to have the browser render the HTML:

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

HTH,
Dan
 
R

Robert Kern

Hello

I'm learning how to call Python scripts through the different
solutions available.

For some reason, this CGI script that I found on Google displays the
contents of the variable but the HTML surrounding it is displayed
as-is by the browser instead of being rendered:

--------------
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

import cgi
form = cgi.FieldStorage()

# get a value from the form
value = form.getvalue("dummy")

print "Content-Type: text/plain;charset=utf-8"
print

# print a document
print "<P>You typed: <TT>%s</TT></P>" % (
cgi.escape(value),
)

By using "Content-Type: text/plain", you told the browser to treat it like plain
text instead of HTML. Use text/html instead.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

Gilles

Thanks all. I (obviously) combined two scripts but didn't notice that
I had to change the "Content-Type" line to output HTML.
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top