提交表单后出现内部服务器错误 flask,sqlite3
internal server error after submitting a form flask,sqlite3
我从表单中插入了新记录,数据将成功添加到数据库中,但它会显示错误页面“内部服务器错误”,如果我手动返回我的表单页面并刷新我可以看到插入数据,但我想让它自己刷新并且不显示错误页面,怎么办?
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
db_name = 'crm.db'
@app.route('/')
def index():
title = "主页"
return render_template("index.html", title = title)
@app.route('/company', methods = ['POST','GET'])
def company():
if request.method == 'GET':
title = "公司"
company_db = query_company()
return render_template("company.html", title = title, company_db = company_db)
else:
insert_company()
def query_company():
connection = sqlite3.connect(db_name)
c = connection.cursor()
c.execute("""SELECT * FROM company""")
company_db = c.fetchall()
return company_db
def insert_company():
connection = sqlite3.connect(db_name)
c = connection.cursor()
query = 'INSERT INTO company(company_full_name, company_short_name) VALUES(?,?)'
cfn = request.form.get("cfn")
csn = request.form.get("csn")
company_info = (cfn,csn)
c.execute(query, company_info)
connection.commit()
connection.close()
if __name__ == '__main__':
app.run(debug = True)
将公司职能更改为...
def company():
if request.method == 'POST':
insert_company()
title = "公司"
company_db = query_company()
return render_template("company.html", title = title, company_db = company_db)
这样会先插入新数据,然后自动刷新所有行
我从表单中插入了新记录,数据将成功添加到数据库中,但它会显示错误页面“内部服务器错误”,如果我手动返回我的表单页面并刷新我可以看到插入数据,但我想让它自己刷新并且不显示错误页面,怎么办?
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
db_name = 'crm.db'
@app.route('/')
def index():
title = "主页"
return render_template("index.html", title = title)
@app.route('/company', methods = ['POST','GET'])
def company():
if request.method == 'GET':
title = "公司"
company_db = query_company()
return render_template("company.html", title = title, company_db = company_db)
else:
insert_company()
def query_company():
connection = sqlite3.connect(db_name)
c = connection.cursor()
c.execute("""SELECT * FROM company""")
company_db = c.fetchall()
return company_db
def insert_company():
connection = sqlite3.connect(db_name)
c = connection.cursor()
query = 'INSERT INTO company(company_full_name, company_short_name) VALUES(?,?)'
cfn = request.form.get("cfn")
csn = request.form.get("csn")
company_info = (cfn,csn)
c.execute(query, company_info)
connection.commit()
connection.close()
if __name__ == '__main__':
app.run(debug = True)
将公司职能更改为...
def company():
if request.method == 'POST':
insert_company()
title = "公司"
company_db = query_company()
return render_template("company.html", title = title, company_db = company_db)
这样会先插入新数据,然后自动刷新所有行