Python CGI取代了我的html

时间:2015-03-12 02:40:57

标签: python html cgi

我正在使用python教自己CGI,我无法找到关于如何将python程序的输出插入到当前html而不是替换整个现有页面的任何引用。


这是python:

#!python
import cgi, cgitb
cgitb.enable()

form = cgi.FieldStorage()

def loan(r, term, pv):
   if r is not None:
      r = float(r)
   if term is not None:
      term = float(term)
   if pv is not None:
      pv = float(pv)
   rate = r / 12
   numPeriods = 12 * term
   rate /= 100
   monthlyPayment = (pv * rate) / (1 - (1 + rate) ** (numPeriods * -1))
   return round(monthlyPayment, 2)

def getInput():
   r = form.getvalue("rate")
   term = form.getvalue("term")
   pv = form.getvalue ("amount")
   monthlyPayment = loan(r, term, pv)
   print("<p>Monthly Payment: %.2f<p>" % monthlyPayment)


print "Content-type: text/html\r\n\r\n"   
print """
<div>
<h1> Python CGI </h1>
<br />
 """

getInput()

print """
<br />
</div>
""" 

HTML:

<DOCTYPE html>
<html>
<head>
<title> Python CGI Test </title>
</head>
<body>

<h1> Mortage Loan Calculator CGI</h1>

<form action ="/cgi-bin/myPython.py" method ="get">

Rate: <input type = "text" name = "rate"/> <br />

Term: <input type = "text" name = "term"/> <br />

Amount <input type = "text" name = "amount"/> <br />

<button type = "submit" > submit </button>
</form>
</body>
</html>

我希望python脚本中的html插入到表单下方。

1 个答案:

答案 0 :(得分:1)

这是服务器端,这意味着一旦加载页面,从Python重新加载内容的唯一方法是通过重新加载或AJAX。所有代码都将在内容发送回用户之前运行。