首页 > 解决方案 > UnboundLocalError:分配前引用的局部变量“参与者”

问题描述

@app.route('/list', methods=['GET', 'POST'])
def list():
    if request.method == 'POST':
        participant = Participant.query.all()
    return render_template('admin/list.html', participant=participant)

标签: pythonvariablesflask

解决方案


错误真的很明显

@app.route('/list', methods=['GET', 'POST'])
def list():
    if request.method == 'POST':
        participant=Participant.query.all()
    return render_template('admin/list.html', participant=participant)

你的变量的范围是问题,你可以通过简单地确保你的return语句位于你的list()函数中来解决这个问题。此外,请确保避免使用为命名函数或变量而保留的 python。


推荐阅读