from flask import Flask, render_template, request,session
from flask import Flask, redirect, url_for
import Distilbert as d
import pyodbc
import pandas as pd
app = Flask(__name__)
app.secret_key = b'_5#ysujsq"Fllinxec]/'
@app.route("/" , methods = ["GET","POST"])
def hello():
return render_template("index.html")
@app.route("/su", methods = ["GET","POST"])
def data_value():
data = []
if request.method == 'POST':
session['context'] = request.form['context']
session['question'] = request.form['question']
data.append(d.Question_answer(session['context'],session['question']))
df = pd.DataFrame(data,columns=['Context', 'Question', 'Answer'])
return render_template("home.html", td = data)
@app.route("/data", methods = ["GET","POST"])
def Insert_data():
if 'context' not in session:
return redirect(url_for('data_value'))
data = [d.Question_answer(session['context'], session['question'])]
df = pd.DataFrame(data, columns=['Context', 'Question', 'Answer'])
if request.method == 'POST':
try:
d.insert_into_db(df, cursor)
print('parsed using normal parsing1_div .')
except Exception as ex:
print('db fail1 ')
print(ex)
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
'Insert_data'的视图函数没有返回有效的响应。函数要么返回None或没有返回语句结束。当我在数据库中插入数据时,我得到了这个错误。
@app.route("/data", methods = ["GET","POST"])
def Insert_data():
...
if request.method == 'POST': # For POST method
...
return render_template("index.html")
# ↓ You need to add this block ↓
else: # For GET method
return <>
如果request.method
不是POST
(实际上这里根本没有return
),则不返回任何内容,但路由接受"GET"
和"POST"
方法