【问题标题】:How to pass data between Python and HTML?如何在 Python 和 HTML 之间传递数据?
【发布时间】:2017-12-03 16:26:03
【问题描述】:

我正在尝试从 HTML 表单中获取输入,在 Python 中对这些输入执行数学运算,然后使用 HTML 标记将结果发布回来。结果将是由返回结果替换的输入表单。

我是初学者,所以我不确定为什么我的代码无法正常工作,因为没有错误。当我单击提交时,该页面仅显示 python 的 505 页面(我认为这是正常的,因为 .py 是可执行的,浏览器无法查看)。

Python

import cgi, cgitb
cgitb.enable()

form = cgi.FieldStorage() 

input1 = form.getvalue('input1')
input2  = form.getvalue('input2')

result = int(input1)+int(input2)
print "Content-Type: text/html\n"
print("<p> this is it " + result + "</p>")

HTML

<form action="calculate.py" method="post" id="singleSequence">
<h4 class="title text-center">Single Calculation</h4>
<h4>Step 1:</h4>
<p>Enter your numbers to add</p>
<p>Input 1 :</p> <input type="text" name="input1"/><br>
<p>Input 2 :</p> <input type="text" name="input2"/><br>    
<input type="submit" value="Send Request">
</form>

【问题讨论】:

    标签: javascript jquery python html cgi


    【解决方案1】:

    我注意到的问题是print("&lt;p&gt; this is it " + result + "&lt;/p&gt;") 行。它引发了TypeError: Can't convert int to string implicitly。使用str(result)

    【讨论】:

    • :(。我看到的唯一其他东西是印刷品。一个带括号,一个没有。也许这就是问题。HTML 的一点:使用&lt;input type="number"&gt;,这使得移动设备更容易。但是,如果打印不是错误,我放弃了...并确保您在测试时发送有效整数..
    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 2021-06-01
    • 2012-09-05
    • 2013-11-30
    • 1970-01-01
    • 2015-04-01
    • 2017-06-10
    • 1970-01-01
    相关资源
    最近更新 更多