Domanda

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!

È stato utile?

Soluzione

I think it is because you didn't call the webapp2.RequestHandler.__init__(). Here is a code snippet from webapp2.py:

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

As you can see the RequestHandler is initialized with request and response. So you have to do same thing in your code:

class BaseHandler(webapp2.RequestHandler):
    def __init__(self, request, response):
        self.tdict = {}
        self.initialize(request, response)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top