如何在提交时获得 html 到 运行 的 python 脚本?
How can I get an html for to run a python script on submit?
我有一个 html 表单,我想使用 python 将信息从表单发送到 sqlite 数据库文件。现在我正在尝试用 cgi 来做。我不需要任何花哨的东西。这是我的代码:
html:
<!DOCTYPE HTML>
<html>
<head>
<title>Sample page to test fill_web_form.py</title>
</head>
<body>
<p1><strong>SAMPLE PAGE TO TEST FILL_WEB_FORM.PY</strong></p1>
<!--test form-->
<form action="send_form_to_db.py" method="post">
Form to be tested:<br>
<input type="text" name="test_form"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
python:
# send_form_to_db.py
# sends data from html form to database
# import statements
import cgi
import sqlite3
cgitb.enable()
form = cg.FieldStorage()
data = form.getvalue('test-form')
conn = sqlite3.connect('sample.db')
conn.execute("INSERT INTO test_table [test_column] VALUES (data)")
conn.commit()
conn.close()
当我 运行 这个网络浏览器不 运行 python 代码时,它只是将其显示为文本。
现在我只是想以最简单的方式来做这件事,而不用像 Django 这样的东西。
小细节:将 form = cg.FieldStorage()
更改为 form = cgi.FieldStorage()
。
注:i in cgi.
我有一个 html 表单,我想使用 python 将信息从表单发送到 sqlite 数据库文件。现在我正在尝试用 cgi 来做。我不需要任何花哨的东西。这是我的代码:
html:
<!DOCTYPE HTML>
<html>
<head>
<title>Sample page to test fill_web_form.py</title>
</head>
<body>
<p1><strong>SAMPLE PAGE TO TEST FILL_WEB_FORM.PY</strong></p1>
<!--test form-->
<form action="send_form_to_db.py" method="post">
Form to be tested:<br>
<input type="text" name="test_form"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
python:
# send_form_to_db.py
# sends data from html form to database
# import statements
import cgi
import sqlite3
cgitb.enable()
form = cg.FieldStorage()
data = form.getvalue('test-form')
conn = sqlite3.connect('sample.db')
conn.execute("INSERT INTO test_table [test_column] VALUES (data)")
conn.commit()
conn.close()
当我 运行 这个网络浏览器不 运行 python 代码时,它只是将其显示为文本。
现在我只是想以最简单的方式来做这件事,而不用像 Django 这样的东西。
小细节:将 form = cg.FieldStorage()
更改为 form = cgi.FieldStorage()
。
注:i in cgi.