我尝试创建一个将数据从Web表单传递到sqlite
数据库的站点。我一直收到500错误。当我输入IP地址并进入main.html时,它会显示没有错误的页面,但是当我提交表单时,我收到500错误。
它没有给我堆栈跟踪只是说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
时,它不会给我一个错误,它只是给了我
在我的浏览器中搜索http://127.0.0.1不起作用,我猜是因为在运行服务器上而不是在我的计算机上运行了烧瓶运行。
这是我的代码:
__ init__.py
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)
forms.py
from flask_wtf import Form
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired
class TestForm(Form):
test_form = StringField('test_form', validators=[DataRequired()])
main.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>
的test.html
<DOCTYPE html>
</html>
<head>
<title>test</title>
</head>
<body>
<h1>Testing 123</h1>
</body>
</html>