webapp2 路由失败
我希望 webapp2 路由会很容易,但显然它不是(对我来说)。
这个问题类似于 webapp2 路由失败,因为代码几乎相同,但是当我使用webapp2.Route 我只收到 404 错误,当我使用 laze 路由时(正如上面提到的其他问题中的解决方案),我收到此彩色错误消息:
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}
)
)
templates/browse.html
<html>
<head>
<title>Success!</title>
</head>
<body>
Success!
{% if test_key %}- {{ test_key }}{% endif %}
</body>
</html>
我做错了什么?非常感谢任何帮助/建议!谢谢!
I hoped that webapp2 routing would be easy but apparently it is not (for me).
This is issue is similar to webapp2 route fails as the code is pretty much the same but when I use webapp2.Route I only get 404 errors and when I use laze routing (as is the solution in the other question mentioned above) I get this colorful error message:
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 -
It would be nice if I could use webapp2.Route due to the extra features this brings (e.g. naming). However, it seems neither is working for me. In short this is how my code looks like:
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 (with webapp2.Route)
import webapp2
import handler
SITE_URLS = [
webapp2.Route(r'^/
urls.py (with webapp2 lazy routing)
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}
)
)
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!! Thanks!
, handler.TestHome),
webapp2.Route(r'^/test/(\w+)
urls.py (with webapp2 lazy routing)
handler.py
templates/browse.html
What am I doing wrong? Any help/suggestions are greatly appreciated!! Thanks!
, handler.TestPage)
]
urls.py (with webapp2 lazy routing)
handler.py
templates/browse.html
What am I doing wrong? Any help/suggestions are greatly appreciated!! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是捕获 url 的正确正则表达式
,您还可以获取命名路由,只需添加即可
,当您有更多参数和/或希望保持代码尽可能干净时,命名路由通常会很有帮助。因此,例如
反映了
有关回溯的信息 - 我这边没有它,但我使用了 GAE prod env(据我所知,您正在开发中),因此请尝试将 webapp2 更新到最新版本,并且确保在 app.yaml
HTH 中定义的相同 py 版本上运行代码。
here's the correct regex to catch your urls
you can also get named route, simply by adding
and named routes are usually helpful when you have more params, and/or prefer to keep your code as clean as possible. so, for example
reflects
regarding that traceback - i don't have it on my side, but i used GAE prod env (as far as i can see you're on dev), so try to update webapp2 to the newest version, and make sure you run the code on the same py version as defined in app.yaml
HTH.