我希望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就好了,因为它带来了额外的功能(例如命名)。不过,这两件事似乎都不适合我。简而言之,我的代码是这样的:
应用程序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"
主.py
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import webapp2
import urls
site_app = webapp2.WSGIApplication(urls.SITE_URLS, debug=True)
url.py(带webapp2.Route)
import webapp2
import handler
SITE_URLS = [
webapp2.Route(r'^/$', handler.TestHome),
webapp2.Route(r'^/test/(\w+)$', handler.TestPage)
]
url.py(使用webapp2延迟路由)
import handler
SITE_URLS = [
('/', handler.TestHome),
('/test/(\w+)', handler.TestPage)
]
处理程序.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>
我做错什么了?非常感谢您的帮助/建议!!谢谢!
最佳答案
这里有正确的regex来捕获您的url
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(据我所见,您在dev上),所以请尝试将webapp2更新到最新版本,并确保您在app.yaml中定义的同一py版本上运行代码
哦。
关于python - webapp2路由失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8481653/