Sudhir Jonathan Sudhir Jonathan - 1 year ago 70
Python Question

How do I make a trailing slash optional with webapp2?

I'm using the new webapp2 (now the default webapp in 1.6), and I haven't been able to figure out how to make the trailing slash optional in code like this:

webapp.Route('/feed', handler = feed)

I've tried
, all to no avail.

Answer Source

To avoid creating duplicate URL:s to the same page, you should use a RedirectRoute with strict_slash set to True to automatically redirect /feed/ to /feed, like this:

from webapp2_extras.routes import RedirectRoute

route = RedirectRoute('/feed', handler=feed, strict_slash=True)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download