python - 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)
解决方案
错误真的很明显
@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。
推荐阅读
- python-3.x - 使用 KDTree/KNN 返回最近的邻居
- macos - 无法在 macOS 10.12 上运行 Qt 5.12.2
- node.js - URL中运算符之间的区别
- ios - 使用 iFunbox > 用户文件系统在 iOS 13.3 上删除空相册
- c++ - 为什么编译器会报告“operator<< and operator>> recursive on all control paths will cause stack overflow”?
- c++ - 具有不同返回类型的函数变体
- sql - 如何显示来自两个公用表表达式的列?
- python - 有没有解决pandas内存错误的方法?
- python - 遍历列表并将计算值插入第 n 个位置
- swift - 从沙盒应用程序启动的应用程序未经过沙盒处理