NameError:不定义全局名称“q”。

时间:2021-10-26 20:27:22

I was able run the Hello World program in the google app engine in below link https://cloud.google.com/appengine/docs/python/

我可以在谷歌应用程序引擎中运行Hello World程序,链接https://cloud.google.com/appengine/docs/python/。

But after I have modified the code for the main.py file (Trying out new handler) I am getting the below error.

但是在我修改了main的代码之后。py文件(尝试新的处理程序)我得到了下面的错误。

Traceback (most recent call last):
  File "/home/natesan/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/natesan/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/natesan/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/natesan/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/natesan/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/natesan/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/home/natesan/webapps/udacity1/main.py", line 29, in get
    self.request.get(q)
NameError: global name 'q' is not defined

The main.py file is as below.

主要的。py文件如下所示。

import webapp2

form="""
<form action="/Testform">
    <input name="q" >
    <input type="submit">
</form>
"""
class TestHandler(webapp2.RequestHandler):
    def get(self):
        self.request.get(q)
        self.response.write(q)

class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.write(form) 

app = webapp2.WSGIApplication([
    ('/', MainHandler),('/Testform',TestHandler)
], debug=True)

Can anyone please help me out what the error is.

谁能帮我解决这个错误。

1 个解决方案

#1


4  

You need to write q in quotes like this: "q".

你需要用这样的引号来写q: q。

Without quotes, q will be treated as a variable and since you haven't declared it, the error is raised.

如果没有引号,q将被视为一个变量,因为您还没有声明它,错误就会被提高。

I modified the TestHandler class as below.

我修改了下面的TestHandler类。

class TestHandler(webapp2.RequestHandler):
    def get(self):
        q=self.request.get("q")
        self.response.write(q)

Assign a value to the variable q=self.request.get("q") and display it in the browser with self.response.write(q)

将一个值赋给变量q=self。request.get(“q”)并在浏览器中显示它。

#1


4  

You need to write q in quotes like this: "q".

你需要用这样的引号来写q: q。

Without quotes, q will be treated as a variable and since you haven't declared it, the error is raised.

如果没有引号,q将被视为一个变量,因为您还没有声明它,错误就会被提高。

I modified the TestHandler class as below.

我修改了下面的TestHandler类。

class TestHandler(webapp2.RequestHandler):
    def get(self):
        q=self.request.get("q")
        self.response.write(q)

Assign a value to the variable q=self.request.get("q") and display it in the browser with self.response.write(q)

将一个值赋给变量q=self。request.get(“q”)并在浏览器中显示它。