Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'request' is not defined

Tags:

python

flask

I got this Python Code, and somehow I get the Error Message:

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') 
like image 396
Leonard Michalas Avatar asked Jan 05 '17 14:01

Leonard Michalas


People also ask

How do you solve NameError name is not defined?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

How do you correct a NameError in Python?

You can fix this by doing global new at the start of the function in which you define it. This statement puts it in the global scope, meaning that it is defined at the module level. Therefore, you can access it anywhere in the program and you will not get that error.

How do I fix NameError name NP not defined?

The Python "NameError: name 'numpy' is not defined" occurs when we use the numpy module without importing it first. To solve the error, install the module and import it ( import numpy ) before using it. Open your terminal in your project's root directory and install the numpy module.

What causes NameError in Python?

In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way. Some of the common mistakes that cause this error are: Using a variable or function name that is yet to be defined.


1 Answers

You appear to have forgotten to import the flask.request request context object:

from flask import request 
like image 156
Martijn Pieters Avatar answered Sep 19 '22 04:09

Martijn Pieters