darkangel
darkangel

Reputation: 41

What's the best practice to use named Route in AppEngine?

In the app.yaml file, I have put 2 lines to specify the url mapping:

the problem is I can't use the "uri_for" function to generate a url for blog module in home.py, case there's no Route added in home moudle:

here is the code in home module:

    app = webapp2.WSGIApplication([
       webapp2.Route(r'/', handler=HomeHandler, name='home')
    ], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)

and code in blog.py:

    app = webapp2.WSGIApplication([
    webapp2.Route(r'/blog/<blog_id:\d+>', handler=BlogHandler,    name="blog")
    ], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)

so, if I have code like this: {{ uri_for('blog', blog_id=blabla) }} in home.html, it can't work.

Upvotes: 1

Views: 112

Answers (1)

John R
John R

Reputation: 753

You should consolidate those routes into one app.

app = webapp2.WSGIApplication([
       webapp2.Route(r'/', handler=HomeHandler, name='home'),
       webapp2.Route(r'/blog/<blog_id:\d+>', handler=BlogHandler, name="blog")
    ], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)

and actually those are only the view blog post routes.

If you wanted to do a full CRUD app, you might need to add some more.

app = webapp2.WSGIApplication([
        webapp2.Route(r'/admin/blog', handler='admin.AdminBlogHandler:list, name="admin.blog.list"),
        webapp2.Route(r'/admin/blog/new', handler='admin.AdminBlogHandler:new', name='admin.blog.edit'),
        webapp2.Route(r'/admin/blog/<id:[^/]+>/edit', handler='admin.AdminBlogHandler:edit', name='admin.blog.edit'),
        webapp2.Route(r'/admin/blog/<id:[^/]+>', handler='admin.AdminBlogHandler:view', name='admin.blog.view')
    ], debug = SITE_CONFIG['is_debug'], config=SITE_CONFIG)

Note for these examples:

1) you prefix a name to load the handlers from a different file (admin.AdminBlogHandler will look in 'admin.py' for 'class AdminBlogHandler'

2) you specify the method to run after the handler name, after the colon.

3) in each method I am creating functionality for get and post, so there are not discrete RESTful URLs for edit and update.

Upvotes: 1

Related Questions