Skip to content

Instantly share code, notes, and snippets.

@nebil
Last active September 10, 2016 15:40
Show Gist options
  • Save nebil/cc5d44c6a4f88559ef2830139f0505a2 to your computer and use it in GitHub Desktop.
Save nebil/cc5d44c6a4f88559ef2830139f0505a2 to your computer and use it in GitHub Desktop.
How to append a slash with Bottle
from bottle import hook, redirect, request
@hook('before_request')
def append_slash():
if request.path.endswith('/'):
pass
else:
redirect(request.path + '/')
# This function could also be written as...
# @hook('before_request')
# def append_slash():
# if not request.path.endswith('/'):
# redirect(request.path + '/')
# But I'm slightly annoyed by how "if not endswith" sounds.
# By the way, Guido, I think this example seemingly evinces that Python
# would benefit from accepting an "unless" keyword, as Perl and Ruby do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment