Domanda

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'^/$', handler.TestHome),
  webapp2.Route(r'^/test/(\w+)$', handler.TestPage)
]

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!

È stato utile?

Soluzione

here's the correct regex to catch your urls

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

you can also get named route, simply by adding

 webapp2.Route(r'/test/<your_route_name_here:\w+>', handler.TestPage)

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

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

reflects

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

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top