我正在尝试创建一个 CGI 表单,允许用户输入一个词,然后它会将这个词发送到下一页(另一个 CGI)。我知道如何使用 .html 文件来完成它,但是当涉及到使用 python/cgi 来完成它时我迷路了。
这是我需要做的,但它是在 html 中。
<html>
<h1>Please enter a keyword of your choice</h1>
<form action="next.cgi" method="get">
Keyword: <input type="text" keyword="keyword"> <br />
<input type="submit" value="Submit" />
</form>
</html>
有谁知道如何用cgi 创建提交按钮?这是我目前所拥有的。
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
keyword = form.getvalue('keyword')
最佳答案
要从 Python cgi 页面显示 html,您需要使用 print 语句。
这是一个使用您的代码的示例。
#!/home/python
import cgi
import cgitb
cgitb.enable()
print 'Content-type: text/html\r\n\r'
print '<html>'
print '<h1>Please enter a keyword of your choice</h1>'
print '<form action="next.cgi" method="get">'
print 'Keyword: <input type="text" name="keyword"> <br />'
print '<input type="submit" value="Submit" />'
print '</form>'
print '</html>'
然后在您的 next.cgi 页面上,您可以获取表单提交的值。像这样的东西:
#!/home/python
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
keyword = form.getvalue('keyword')
print 'Content-type: text/html\r\n\r'
print '<html>'
print keyword
print '</html>'
关于python - 使用python的CGI表单提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13814241/