[英]i am getting this error : " UnboundLocalError: local variable 'Requesting_books' referenced before assignment "
i want to use the variable " Requesting_books ", but everytime i am getting this error : "UnboundLocalError: local variable 'Requesting_books' referenced before assignment".我想使用变量“Requesting_books”,但每次我收到此错误时:“UnboundLocalError:分配前引用的局部变量‘Requesting_books’”。 Actually i want to use this variable in my jinja template so that i can show search results.
实际上我想在我的 jinja 模板中使用这个变量,以便我可以显示搜索结果。 Please Help me !
请帮我 !
@app.route('/request books', methods=['GET', 'POST'])
@app.route('/get books', methods=['GET', 'POST'])
def requesting_for_books():
if request.method == 'POST':
requesting = mongo.db.mylogin
Requesting_books = requesting.find_one({'name' : request.form['bookname']})
return render_template('get_books.html', title="Get Books", my_book=Requesting_books['name'])
You should add an else
block because if request.method
is not 'POST'
no variable will be assigned .您应该添加一个
else
块,因为如果request.method
不是'POST'
则不会分配变量。
@app.route('/request books', methods=['GET', 'POST'])
@app.route('/get books', methods=['GET', 'POST'])
def requesting_for_books():
if request.method == 'POST':
requesting = mongo.db.mylogin
Requesting_books = requesting.find_one({'name' : request.form['bookname']})
return render_template('get_books.html', title="Get Books", my_book=Requesting_books['name']) #<--- Move return into the if block
else:
#Do something if request.method is not "POST"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.