为什么我在尝试连接到sqlite数据库时出现500错误?

2024-03-29 09:00:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个将数据从web表单传递到sqlite数据库的站点。我一直收到500个错误。当我输入ip地址主.html,它显示的页面没有错误,但是当我提交表单时,我得到了500个错误。在

它没有给我stacktrace只是说The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

这很难调试,因为当我通过浏览器访问服务器时,它并没有提供有关错误的详细信息。当我在putty中运行flask run时,它不会给我一个错误,它只会给我这个enter image description here 在我的浏览器中搜索http://127.0.0.1不起作用,我猜是因为FlaskRun是在服务器上运行的,而不是在我的计算机上。在

这是我的代码:

初始值

from flask import Flask, render_template
from forms import TestForm
import sqlite3

app = Flask(__name__)


@app.route('/')
def homepage():
    return render_template("main.html", form=TestForm())

@app.route('/post', methods=['POST'])
def post():
    conn = sqlite3.connect("sample.db")//If I remove these two lines
    conn.close()                       //The code works without error
    return render_template("test.html")

if __name__ == "__main__":
    app.run(debug=True)

在表单.py在

^{pr2}$

在主.html在

<DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Sample Page</title>
    <meta name="viewport" content="width=device-width"
    initial=scale=1/>
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='favicon.ico') }}" rel="shortcut icon">

</head>
<h2>Hello, this site is meant to test my fill_web_form.py script</h2>
<br>
<br>
<h1>Test Form</h1>
<form action="/post" method="post" name="test">
    {{ form.test_form(size=80) }}
    <p>Form</p><br>
      <p><input type="submit" value="Test Form"></p>
 </form>
</html>

在测试.html在

<DOCTYPE html>
</html>
<head>
    <title>test</title>
</head>
<body>
    <h1>Testing 123</h1>
</body>
</html>

Tags: nametestimportformapp表单titleis