webapp2路由失败

时间:2011-12-12 22:14:21

标签: python google-app-engine webapp2

我希望webapp2路由很容易,但显然不适合(对我而言)。

这个问题类似于webapp2 route fails,因为代码几乎相同,但是当我使用webapp2.Route时,我只得到404错误,当我使用laze routing时(在提到的另一个问题中的解决方案)上面)我收到这个色彩缤纷的错误信息:

ERROR    2011-12-12 17:09:25,996 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 1076, in __call__
    handler = self.handler(request, response)
TypeError: __init__() takes exactly 1 argument (3 given)
INFO     2011-12-12 17:09:26,061 dev_appserver.py:2753] "GET / HTTP/1.1" 500 -
INFO     2011-12-12 17:09:26,606 dev_appserver.py:2753] "GET /favicon.ico HTTP/1.1" 200 -

如果我可以使用webapp2.Route会很好,因为它带来了额外的功能(例如命名)。然而,似乎两者都不适合我。简而言之,这就是我的代码的样子:

的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(使用webapp2.Route)

import webapp2

import handler

SITE_URLS = [
  webapp2.Route(r'^/$', handler.TestHome),
  webapp2.Route(r'^/test/(\w+)$', handler.TestPage)
]

urls.py(使用webapp2延迟路由)

import handler

SITE_URLS = [
  ('/', handler.TestHome),
  ('/test/(\w+)', handler.TestPage)
]

handler.py

import os
import webapp2
from google.appengine.ext.webapp import template

class TestHome(webapp2.RequestHandler):

  def get(self):
    self.response.write(template.render(
        os.path.join(os.path.dirname(__file__), 'templates/browse.html'), {}
      )
    )


class TestPage(webapp2.RequestHandler):

  def get(self, test_key):
    self.response.write(template.render(
        os.path.join(os.path.dirname(__file__), 'templates/browse.html'),
        {'test_key': test_key}
      )
    )

模板/ browse.html

<html>
<head>
  <title>Success!</title>
</head>
<body>
Success!
{% if test_key %}- {{ test_key }}{% endif %}
</body>
</html>

我做错了什么?任何帮助/建议都非常感谢!!谢谢!

1 个答案:

答案 0 :(得分:6)

这是抓住你网址的正确正则表达式

SITE_URLS = [
    webapp2.Route(r'/', handler.TestHome),
    webapp2.Route(r'/test/<:\w+>', handler.TestPage)
]

您也可以通过添加

来获取命名路线
 webapp2.Route(r'/test/<your_route_name_here:\w+>', handler.TestPage)
当你有更多参数时,

和命名路由通常很有用,并且/或者更喜欢保持代码尽可能干净。所以,例如

webapp2.Route(r'/test/<category:\w+>/<user_id>/<day:\d+>', handler.TestPage)

反映

class TestPage(webapp2.RequestHandler):
    def get(self, category, user_id, day):
        ...

关于那个追溯 - 我没有它在我身边,但我使用了GAE prod env(据我所知,你在开发中),所以尝试将webapp2更新到最新版本,并确保您在app.yaml

中定义的相同py版本上运行代码

HTH。