我仍在使用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')
模板/ browse.html
<html>
<head>
<title>Success!</title>
</head>
<body>
Success!
{% if test_key %}- {{ test_key }}{% endif %}
</body>
</html>
我做错了什么?非常感谢任何帮助/建议!
答案 0 :(得分:9)
我认为这是因为你没有打电话给webapp2.RequestHandler.__init__()
。以下是webapp2.py的代码段:
class RequestHandler(object):
# ...
def __init__(self, request=None, response=None):
self.initialize(request, response)
正如您所看到的,RequestHandler
已初始化为request
和response
。所以你必须在你的代码中做同样的事情:
class BaseHandler(webapp2.RequestHandler):
def __init__(self, request, response):
self.tdict = {}
self.initialize(request, response)