我仍然遇到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>


我究竟做错了什么?任何帮助/建议都将不胜感激!

最佳答案

我认为这是因为您没有调用webapp2.RequestHandler.__init__()。这是webapp2.py的代码片段:

class RequestHandler(object):
    # ...
    def __init__(self, request=None, response=None):
        self.initialize(request, response)


如您所见,RequestHandlerrequestresponse初始化。因此,您必须在代码中做同样的事情:

class BaseHandler(webapp2.RequestHandler):
    def __init__(self, request, response):
        self.tdict = {}
        self.initialize(request, response)

关于python - AttributeError:'NoneType'对象没有属性'route'和webapp2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8488482/

10-11 04:11