gpt4 book ai didi

Python CGI 替换我的 html

转载 作者:行者123 更新时间:2023-11-28 01:34:29 25 4
gpt4 key购买 nike

我正在用 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 插入到表单下方。

最佳答案

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

关于Python CGI 替换我的 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com