pombreda / webapp-improved

Automatically exported from code.google.com/p/webapp-improved
Other
0 stars 0 forks source link

webapp2.uri_for unable to build URI due to missing keyword argument #76

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Add the following route to your app:

app = webapp2.WSGIApplication([
    webapp2.Route(r'/logout/<page_id:.*>', handler=views.LogoutPage, name='logout', defaults={'page_id': ''})
], debug=True)

2. Add the following view to your app (located in views.py):

class LogoutPage(webapp2.RequestHandler):
    def get(self, page_id=''):
        return self.redirect('/' + page_id)

3. Create a custom Jinja2 template filter like this:

def url(name, **kwargs):
    return webapp2.uri_for(name, **kwargs)

4. Create a Jinja2 template with the following line:

<a href="{{ 'logout'|url(page_id='') }}">logout</a>

What is the expected output? What do you see instead?
I expect to see the correct URI for the handler when the template is rendered.  
Instead I get this:

  File "/base/data/home/apps/s~myapp/5/templates/base_back_admin.html", line 12, in block "link_area"
   <a href="{{ 'logout'|url(page_id='') }}">logout</a>
  File "/base/data/home/apps/s~myapp/5/tags.py", line 368, in url
    return webapp2.uri_for(name, **kwargs)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1706, in uri_for
    return request.app.router.build(request, _name, args, kwargs)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1224, in default_builder
    return route.build(request, args, kwargs)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 978, in build
    path, query = self._build(args, kwargs)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1002, in _build
    name.strip('_'))
KeyError: 'Missing argument "page_id" to build URI.'

What version of the product are you using? On what operating system?
I am using the webapp2 module included in the GAE SDK v1.7.5 with Python 2.7 on 
a Mac OS X Snow Leopard (10.6.8).

Please provide any additional information below.
This works flawlessly in the development SDK but fails with the above error on 
GAE production servers.  As you can see from my code, I used the defaults 
parameter in the url pattern to send a default page_id argument to the view.  I 
also gave the page_id parameter in the view definition a default value.  Then I 
even sent the page_id value manually as an argument in the url function called 
from the template filter.  I tried every combination of one or more of these 
three methods, but the production app still claims the page_id argument is 
missing.  I don't know what to do since this works on the development server.

Original issue reported on code.google.com by cpatters...@gmail.com on 5 Mar 2013 at 11:31