[英]“NameError: name 'redirect' is not defined” even though redirect is imported
I have this simple Flask 1.1.x app:我有这个简单的 Flask 1.1.x 应用程序:
from flask import Flask
from flask import redirect, render_template, url_for
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
return render_template("index.html")
@app.route('/donate', methods=['GET'])
def donate():
return render_template("donate.html")
@app.route('/donations', methods=['GET'])
def donations():
return redirect(url_for('donate'))
if __name__ == '__main__':
app.run(host='localhost', port=8080, debug=True)
I'm developing on a local server.我正在本地服务器上开发。 When I attempt to access
/donations
in a browser, I get an Internal Server Error and the local server reports this error:当我尝试在浏览器中访问
/donations
时,我收到内部服务器错误并且本地服务器报告此错误:
ERROR in app: Exception on /donations [GET]
<trimmed>
return redirect(url_for('donate'))
NameError: name 'redirect' is not defined
I don't understand why it's reporting redirect
as undefined, since I am importing it and I'm calling it the same way the Flack Quickstart Guide does.我不明白为什么它报告
redirect
为未定义,因为我正在导入它并且我以与Flack 快速入门指南相同的方式调用它。
Could you replace return redirect(url_for('donate'))
with return redirect('/donate')
?你能用 return
return redirect('/donate')
替换return redirect(url_for('donate'))
) 吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.