FLASK-python ValueError:查看函数没有返回响应
问题描述:
我想运行一个将数据插入到表中的sql函数。我下面的例子来说明here但每当我运行该脚本,我得到的错误“ValueError异常:查看功能并没有返回响应”FLASK-python ValueError:查看函数没有返回响应
我的代码如下所示:
from flask import render_template, flash, redirect, request
from app import app
from .forms import LoginForm
from .forms import RegistrationForm
import sqlite3 as sql
@app.route('/')
@app.route('/index')
@app.route('/registration', methods = ['GET','POST'])
def registration():
form = RegistrationForm()
if request.method == 'POST':
try:
card_id = request.form['card_id']
pin = request.form['pin']
account_id = request.form['account_id']
with sql.connect("testDB.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id))
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("index.html",msg = msg)
con.close()
我能有什么可能做错了吗?
答
如果请求方法是GET,会返回什么?您仅在POST请求上呈现模板。纠正它。
@app.route('/registration', methods = ['GET','POST'])
def registration():
form = RegistrationForm()
if request.method == 'POST':
try:
card_id = request.form['card_id']
pin = request.form['pin']
account_id = request.form['account_id']
with sql.connect("testDB.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id))
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("index.html",msg = msg)
con.close()
else:
return render_template('index.html', form=form)
在代码中包含方法'GET'后(请参阅编辑的代码)。我仍然得到相同的错误 – tapeli
看到我的代码最后两行,粘贴并告诉我。 – Sagar
错误消失了,但表单没有将数据插入表中。 – tapeli