属性错误:“NoneType”对象没有属性“route”;和网络应用程序2
我仍然遇到 webapp2 错误,并且不知道这里可能存在什么问题。
ERROR 2011-12-13 11:17:19,059 webapp2.py:1528] 'NoneType' object has no attribute 'route'
Traceback (most recent call last):
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 526, in dispatch
method_name = request.route.handler_method
AttributeError: 'NoneType' object has no attribute 'route'
ERROR 2011-12-13 11:17:19,060 wsgi.py:186]
Traceback (most recent call last):
File "/home/user/sdk/google_appengine/google/appengine/runtime/wsgi.py", line 174, in Handle
result = handler(self._environ, self._StartResponse)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 526, in dispatch
method_name = request.route.handler_method
AttributeError: 'NoneType' object has no attribute 'route'
请注意,我使用的是 GAE 的最新 SDK 版本 (1.6.0)。
我的代码如下所示:
app.yaml
application: test-app
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /favicon\.ico
static_files: static/images/favicon.ico
upload: static/images/favicon\.ico
- url: .*
script: main.site_app
login: required
libraries:
- name: django
version: "1.2"
main.py
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import webapp2
import urls
site_app = webapp2.WSGIApplication(urls.SITE_URLS, debug=True)
urls.py
import webapp2
import handler
SITE_URLS = [
webapp2.Route(r'/', handler.TestHome),
webapp2.Route(r'/test/<test_key:\w+>', handler.TestPage)
]
basehandler.py
import os
import webapp2
from django import template
class BaseHandler(webapp2.RequestHandler):
def __init__(self, *args, **kwargs):
self.tdict = {}
def render(self, template_file):
template_path = os.path.join(os.path.dirname(__file__),
'templates',
template_file)
t = template.Template(file(template_path,'rb').read())
self.response.write(t.render(template.Context(self.tdict)))
handler.py
import os
from basehandler import BaseHandler
class TestHome(BaseHandler):
def get(self):
def get(self):
self.render('browse.html')
class TestPage(BaseHandler):
def get(self, test_key):
self.tdict['test_key'] = test_key
self.render('browse.html')
templates/browse.html
<html>
<head>
<title>Success!</title>
</head>
<body>
Success!
{% if test_key %}- {{ test_key }}{% endif %}
</body>
</html>
我做错了什么?非常感谢任何帮助/建议!
I am still running into errors with webapp2 and am at a lost what could be the problem here.
ERROR 2011-12-13 11:17:19,059 webapp2.py:1528] 'NoneType' object has no attribute 'route'
Traceback (most recent call last):
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 526, in dispatch
method_name = request.route.handler_method
AttributeError: 'NoneType' object has no attribute 'route'
ERROR 2011-12-13 11:17:19,060 wsgi.py:186]
Traceback (most recent call last):
File "/home/user/sdk/google_appengine/google/appengine/runtime/wsgi.py", line 174, in Handle
result = handler(self._environ, self._StartResponse)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/home/user/sdk/google_appengine/lib/webapp2/webapp2.py", line 526, in dispatch
method_name = request.route.handler_method
AttributeError: 'NoneType' object has no attribute 'route'
Please note that I am using the latest SDK version (1.6.0) of GAE.
My code looks like this:
app.yaml
application: test-app
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /favicon\.ico
static_files: static/images/favicon.ico
upload: static/images/favicon\.ico
- url: .*
script: main.site_app
login: required
libraries:
- name: django
version: "1.2"
main.py
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import webapp2
import urls
site_app = webapp2.WSGIApplication(urls.SITE_URLS, debug=True)
urls.py
import webapp2
import handler
SITE_URLS = [
webapp2.Route(r'/', handler.TestHome),
webapp2.Route(r'/test/<test_key:\w+>', handler.TestPage)
]
basehandler.py
import os
import webapp2
from django import template
class BaseHandler(webapp2.RequestHandler):
def __init__(self, *args, **kwargs):
self.tdict = {}
def render(self, template_file):
template_path = os.path.join(os.path.dirname(__file__),
'templates',
template_file)
t = template.Template(file(template_path,'rb').read())
self.response.write(t.render(template.Context(self.tdict)))
handler.py
import os
from basehandler import BaseHandler
class TestHome(BaseHandler):
def get(self):
def get(self):
self.render('browse.html')
class TestPage(BaseHandler):
def get(self, test_key):
self.tdict['test_key'] = test_key
self.render('browse.html')
templates/browse.html
<html>
<head>
<title>Success!</title>
</head>
<body>
Success!
{% if test_key %}- {{ test_key }}{% endif %}
</body>
</html>
What am I doing wrong? Any help/suggestions are greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是因为您没有调用
webapp2.RequestHandler.__init__()
。 是 webapp2.py 中的代码片段:以下 您可以看到
RequestHandler
已使用request
和response
进行初始化。所以你必须在代码中做同样的事情:I think it is because you didn't call the
webapp2.RequestHandler.__init__()
. Here is a code snippet from webapp2.py:As you can see the
RequestHandler
is initialized withrequest
andresponse
. So you have to do same thing in your code: