Friday, November 10, 2017

[Flask][Resolved] NameError: name 'request' is not defined

Error message

C:\python\flask\tutsplus.com>python app.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2018-01-11 12:38:56,004] ERROR in app: Exception on /signUp [POST]
Traceback (most recent call last):
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-pac
kages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "app.py", line 14, in signUp
    _name = request.form['inputName'] # read the posted values from the UI
NameError: name 'request' is not defined
127.0.0.1 - - [11/Jan/2018 12:38:56] "POST /signUp HTTP/1.1" 500 -

Example source code

from flask import Flask
app = Flask(__name__) #create an app using Flask as shown

@app.route('/',methods=['POST'])
def main():
    _name = request.form['inputName'] # read the posted values from the UI
    _email = request.form['inputEmail']
    _password = request.form['inputPassword']
    
if __name__ == "__main__": #check if the executed file is the main program
    app.run() #run the app

Solution


Since request is used, so you need to check request is imported:
from flask import Flask, request
app = Flask(__name__) #create an app using Flask as shown

@app.route('/',methods=['POST'])
def main():
    _name = request.form['inputName'] # read the posted values from the UI
    _email = request.form['inputEmail']
    _password = request.form['inputPassword']
    
if __name__ == "__main__": #check if the executed file is the main program
    app.run() #run the app

No comments :

Post a Comment