Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [CGI] Why is HTML not rendered?

Reply
Thread Tools

[CGI] Why is HTML not rendered?

 
 
Gilles
Guest
Posts: n/a
 
      08-17-2012
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),
)
--------------

Here's the output:
--------------
<P>You typed: <TT>test</TT></P>
--------------

Could this be due to the script itself, or some server configuration?

Thank you.
 
Reply With Quote
 
 
 
 
Alexander Blinne
Guest
Posts: n/a
 
      08-17-2012
On 17.08.2012 15:27, Gilles wrote:
> 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.

 
Reply With Quote
 
 
 
 
Dan Sommers
Guest
Posts: n/a
 
      08-17-2012
On 2012-08-17 at 15:27:59 +0200,
Regarding "[CGI] Why is HTML not rendered?,"
Gilles <(E-Mail Removed)> wrote:

> 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

--
Μὴ μοῦ τοὺς κύκλους τάραττε -- Αρχιμηδησ
Do not disturb my circles. -- Archimedes

Dan Sommers, http://www.tombstonezero.net/dan
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      08-17-2012
On 8/17/12 2:27 PM, Gilles wrote:
> 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),
> )
> --------------
>
> Here's the output:
> --------------
> <P>You typed: <TT>test</TT></P>
> --------------
>
> Could this be due to the script itself, or some server configuration?


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

 
Reply With Quote
 
Gilles
Guest
Posts: n/a
 
      08-17-2012
On Fri, 17 Aug 2012 14:44:37 +0100, Robert Kern
<(E-Mail Removed)> wrote:
>> 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


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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
firefox html, my downloaded html and firebug html different? Adam Akhtar Ruby 9 08-16-2008 07:55 PM
Why :: ? Why not : ? Why not . ? <- less clutter ?!? Skybuck Flying C++ 16 08-25-2007 09:48 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM



Advertisments