knight knight - 2 months ago 10
HTML Question

Python newline not working from form data to string formating

I am new to Python and trying server side form handling. I have a simple html page with three input fields and and a textarea. When user fill in the details the form is processed and user presented with all the details he filled in. My only issue is that I can't get new line to work while printing the data in the main program after htmlTop() function. Tried many ways but without luck.
Following is my code that I have tried so far.




#!C:\Python34\python.exe

import cgi

def htmlTop():
print("""Content-type:text/html\n\n
<!Doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title> Server Template </title>
</head>
<body>""")

def htmlTail():
print("""</body>
</html>""")

def getData():
formData = cgi.FieldStorage()
firstname = formData.getvalue('firstName')
lastname = formData.getvalue("lastName")
email = formData.getvalue('email');
message = formData.getvalue('message')
dataList = [firstname , lastname , email , message]
return dataList


#main program construct

if __name__ == "__main__":
try:
htmlTop()
data = getData();
print("Welcome! {1} {0}".format(data[0],data[1]))
print("\n" + "Your email is {0}".format(data[2]))
print("\n" + "Your entered the following message: {0}".format(data[3]))

htmlTail()
except:
cgi.print_exception()

Answer

Try using <br> to add a line break instead of a newline character.