简体   繁体   English

如何在提交时获取运行python脚本的html?

[英]How can I get an html for to run a python script on submit?

I have an html form and I want to use python to send the information from the form to an sqlite database file. 我有一个HTML表单,我想使用python将表单中的信息发送到sqlite数据库文件。 Right now I am trying to do it with cgi. 现在我正在尝试用cgi做。 I don't need anything fancy. 我不需要任何花哨的东西。 Here is my code: 这是我的代码:

html: 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()

When I run this the web browser doesn't run the python code, it just displays it as text. 当我运行它时,Web浏览器不运行python代码,它只是将其显示为文本。

Right now I'm just trying to do this in the simplest way possible without implementing things like Django. 现在我只是想以最简单的方式做到这一点,而不用像Django这样的东西。

Minor detail: change form = cg.FieldStorage() to form = cgi.FieldStorage() . 次要细节:将form = cg.FieldStorage()更改为form = cgi.FieldStorage()

Note: i in cgi. 注意:我在cgi中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM