Python NameError:未定义名称“请求”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41487473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NameError: name 'request' is not defined
提问by Leonard Michalas
I got this Python Code, and somehow I get the Error Message:
我得到了这个 Python 代码,不知何故我得到了错误消息:
File "/app/identidock.py", line 13, in mainpage if request.method == 'POST': NameError: name 'request' is not defined
File "/app/identidock.py", line 13, in mainpage if request.method == 'POST': NameError: name 'request' is not defined
But I really can't find my mistake. Can someone please help me with that?
但我真的找不到我的错误。有人可以帮我吗?
from flask import Flask, Response
import requests
import hashlib
app = Flask(__name__)
salt = "UNIQUE_SALT"
default_name = 'test'
@app.route('/', methods=['GET', 'POST'])
def mainpage():
name = default_name
if request.method == 'POST':
name = request.form['name']
salted_name = salt + name
name_hash = hashlib.sha256(salted_name.encode()).hexdigest()
header = '<html><head><title>Identidock</title></head><body>'
body = '''<form method="POST">
Hallo <input type="text" name="name" value="{0}">
<input type="submit" value="Abschicken">
</form>
<p> Du siehst aus wie ein: </p>
<img src="/monster/{1}"/>
'''.format(name, name_hash)
footer = '</body></html>'
return header + body + footer
@app.route('/monster/<name>')
def get_identicon(name):
r = requests.get('http://dnmonster:8080/monster/' \
+ name + '?size=80')
image = r.content
return Response(image, mimetype='image/png')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
回答by Martijn Pieters
You appear to have forgotten to import the flask.request
request contextobject:
您似乎忘记导入flask.request
请求上下文对象:
from flask import request
回答by Willem Van Onsem
You are probably missing the following import
statement:
您可能缺少以下import
语句:
from flask import request
that should be placed in the header of the file.
应该放在文件的标题中。
回答by Shoaib Malek
Use this will Work,
使用这将工作,
self.request
自我请求
回答by yasin lachini
install request
安装 request
pip install request
and add
并添加
from flask import request
to the first line of project
到项目的第一行