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

sudipto manna sudiptod19 at gmail.com
Sun Jan 17 21:13:44 EST 2016


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()

  elif request.method == 'POST':
   return makeANewPuppy()

File# FlaskTendpointTester2.py
import httplib2
import json
import sys

print "Running Endpoint Tester....\n"
address = raw_input("Please enter the address of the server you want to
access, \n If left blank the connection will be set to '
http://localhost:5000':   ")
if address == '':
address = 'http://localhost:5000'
#Making a GET Request
print "Making a GET Request for /puppies..."

Virtual machine is running on port:5000

and the error trace seen on when the endpoint code is executed on localhost
port:5000

Traceback (most recent call last):

  File
"/Users/sudiptomanna/anaconda2/lib/python2.7/site-packages/flask/app.py",
line 1836, in __call__

    return self.wsgi_app(environ, start_response)

  File
"/Users/sudiptomanna/anaconda2/lib/python2.7/site-packages/flask/app.py",
line 1820, in wsgi_app

    response = self.make_response(self.handle_exception(e))

  File
"/Users/sudiptomanna/anaconda2/lib/python2.7/site-packages/flask/app.py",
line 1403, in handle_exception

    reraise(exc_type, exc_value, tb)

  File
"/Users/sudiptomanna/anaconda2/lib/python2.7/site-packages/flask/app.py",
line 1817, in wsgi_app

    response = self.full_dispatch_request()

  File
"/Users/sudiptomanna/anaconda2/lib/python2.7/site-packages/flask/app.py",
line 1477, in full_dispatch_request

    rv = self.handle_user_exception(e)

  File
"/Users/sudiptomanna/anaconda2/lib/python2.7/site-packages/flask/app.py",
line 1381, in handle_user_exception

    reraise(exc_type, exc_value, tb)

  File
"/Users/sudiptomanna/anaconda2/lib/python2.7/site-packages/flask/app.py",
line 1475, in full_dispatch_request

    rv = self.dispatch_request()

  File
"/Users/sudiptomanna/anaconda2/lib/python2.7/site-packages/flask/app.py",
line 1461, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

  File "/Users/sudiptomanna/fullstack/vagrant/PythonData/FlaskTest2.py",
line 8, in puppiesFunction

    if request.method == 'GET':

NameError: global name 'request' is not defined


Regards,

Sudipto Manna

On Sun, Jan 17, 2016 at 7:17 PM, Alan Gauld <alan.gauld at btinternet.com>
wrote:

> On 17/01/16 23:15, sudipto manna wrote:
> > Hi All,
> > I am running a python flask project for fetching the endpoint.
> >
> > Please find the files attached.
>
> OK, This is a text based mailing list so attachments tend
> not to make it through the server. If they are not too long post them
> here, or if they are bigger put them on a pastebin. However...
>
> Flask is not part of the standard library so a little bit
> off topic for this list, you may be better off asking on
> the Flask support forum.
>
> Howe er it is a common framework so somebody here might
> be able to help.
>
> > When the endpoint file is executed its returning this error:
> > "if request.method == 'GET': NameError: global name 'request' is not
> > defined"
>
> We really need to see the full error trace and the full
> context around the reported fault - ideally the full file.
> The probability is that the error is telling the truth
> and 'request' is not defined yet. Is it part of Flask?
> Should you have prepended the module name?
> Is the spelling/case correct?
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list