使用python的CGI表单提交按钮 [英] CGI form submit button using python
问题描述
我正在尝试创建一个cgi表单,该表单将允许用户键入一个单词,然后它将使用该单词并将其发送到下一页(另一个cgi)。我知道如何使用.html文件执行此操作,但是使用python / cgi进行操作时我迷路了。
I am trying to create a cgi form that will allow a user to type in a word and then it will take that word and send it off to the next page (another cgi). I know how to do it with a .html file but I am lost when it comes to doing it with python/cgi.
这是我需要做的,但是它
Here is what I need to do but it is in 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创建一个提交按钮吗?
Does anyone know how to create a submit button with cgi? Here is what I have so far.
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
keyword = form.getvalue('keyword')
推荐答案
要显示Python cgi页面中的html,您需要使用print语句。
To display html from a Python cgi page you need to use the print statement.
以下是使用您的代码的示例。
Here is an example using your code.
#!/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页面上,您可以获取表单提交的值。像这样的东西:
Then on your next.cgi page you can get the values submitted by the form. Something like:
#!/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的CGI表单提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!