Python Flask redirect-UnboundLocalError:赋值错误之前引用的局部变量
奇怪的是,这段代码昨天似乎还在工作,现在我在运行应用程序时遇到了一个Python Flask redirect-UnboundLocalError:赋值错误之前引用的局部变量,python,python-3.x,redirect,flask,return,Python,Python 3.x,Redirect,Flask,Return,奇怪的是,这段代码昨天似乎还在工作,现在我在运行应用程序时遇到了一个UnboundLocalError:assignment之前引用的局部变量“query”错误。从我所看到的,代码在重定向发生之前清楚地定义了查询变量。我有点不知所措 有人看到什么问题吗 ####Index Page @app.route('/', methods=['GET', 'POST']) @app.route('/index', methods=['GET', 'POST']) def index():
UnboundLocalError:assignment之前引用的局部变量“query”
错误。从我所看到的,代码在重定向发生之前清楚地定义了查询变量。我有点不知所措
有人看到什么问题吗
####Index Page
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
exception = ""
try:
connectToDB()
except:
exception = 'Failure to connect to db'
form = StaffNames()
ranumber = form.ranumber.data
if not exception:
if form.validate_on_submit():
query = {
'staff': dict(staff_choices).get(form.staff.data),
'ranumber': form.ranumber.data,
'rai_number' : columnsearch(ranumber)["rai_number"],
'carrier_format' : columnsearch(ranumber)["carrier_format"],
'physical_location' : columnsearch(ranumber)["physical_location"],
'brand_title' : columnsearch(ranumber)["brand_title"],
'recording_artist' : columnsearch(ranumber)["recording_artist"],
'producer' : columnsearch(ranumber)["producer"],
'session' : columnsearch(ranumber)["session"],
'tx_date' : columnsearch(ranumber)["tx_date"],
}
return redirect(url_for('results', **query))
return render_template(
'index.html', title='Search Page', exception=exception, form=form
)
如果
form.validate\u on_submit()
的计算结果为False
,则未定义query
检查你的缩进 你的压痕到处都是。检查制表符和空格的混合。好的,我已经修改了,但是我仍然不能加载索引页。现在缩进是固定的,它非常清晰<如果form.validate on_submit()为false,则在
时未定义代码>查询。好的,为响应干杯。问题是'form.validate_on_submit()'与表单中的[DataRequired()]验证器相关。在评估用户是否输入了任何数据之前,需要加载页面。在任何人都有机会在表单中输入数据并点击提交之前,我看不出它如何计算为false。@bms9nmh:但事实就是这样。调试发布的数据。至少要正确缩进return
语句。啊,是的……对不起,我的return语句缩进不正确。谢谢你们的帮助。