简体   繁体   English

我收到此错误:“UnboundLocalError:分配前引用的局部变量‘Requesting_books’”

[英]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.

相关问题 我收到一个错误:“UnboundLocalError:分配前引用了局部变量'text_to_print'” - I am getting an error: “UnboundLocalError: local variable 'text_to_print' referenced before assignment” 为什么我收到错误:UnboundLocalError: local variable 'lcm' referenced before assignment - Why am I getting the error: UnboundLocalError: local variable 'lcm' referenced before assignment 如何修复“UnboundLocalError:赋值前引用的局部变量‘books’”? - How to fix "UnboundLocalError: local variable 'books' referenced before assignment"? 获取 UnboundLocalError:在赋值错误之前引用了局部变量 - getting UnboundLocalError: local variable referenced before assignment error 为什么在Python中赋值之前会收到UnboundLocalError消息,该消息指出局部变量“参与者”被引用? - Why am I getting the UnboundLocalError that says local variable 'participants' referenced before assignment in Python? UnboundLocalError:赋值前引用了局部变量“i” - UnboundLocalError: local variable 'i' referenced before assignment UnboundLocalError:赋值前引用的局部变量'error' - UnboundLocalError: local variable 'error' referenced before assignment UnboundLocalError: 赋值前引用的局部变量错误 - UnboundLocalError: local variable referenced before assignment error 我收到错误消息:UnboundLocalError:分配前引用了本地变量&#39;porc&#39; - I got error : UnboundLocalError: local variable 'porc' referenced before assignment 赋值之前引用了unboundlocalerror局部变量&#39;i&#39; - unboundlocalerror local variable 'i' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM