[Tutor] if request.method == 'GET': NameError: global name 'request' is not defined

Danny Yoo dyoo at hashcollision.org
Mon Jan 18 04:03:45 EST 2016


On Sun, Jan 17, 2016 at 6:13 PM, sudipto manna <sudiptod19 at gmail.com> wrote:
> Here is the code snippet:
>
> File#FlaskTest2.py
>
> from flask import Flask
>
> app = Flask(__name__)
>
> #Make an app.route() decorator here
> @app.route("/puppies/", methods = ['GET' , 'POST'])
> def puppiesFunction():
>   if request.method == 'GET':
>   return getAllPuppies()


Hi Sudipto,

I don't know the Flask framework here, but this code looks suspicious.
What is "request" in this puppiesFunction definition?  It's not a
parameter.  Where does it come from?

Is it a global?

    http://flask.pocoo.org/docs/0.10/quickstart/#accessing-request-data

... oh.  It is a module global.  That is not something I like
philosophically, but oh well, roll with it...


Do you have other working examples from prior code that used requests?
 If so, you may want to double check them.  You're missing a
definition that you need to add to your program.  Take a look at the
documentation: read the "The Request Object" section in the docs, and
you should see the missing line that you need to add to your program.


If you can't find it after you've read this quickstart documentation,
please feel free to ask the mailing list.  But you should be able to
see it: it's the first boxed piece of code at:

    http://flask.pocoo.org/docs/0.10/quickstart/#the-request-object


Good luck!


More information about the Tutor mailing list