Last active
September 10, 2016 15:40
-
-
Save nebil/cc5d44c6a4f88559ef2830139f0505a2 to your computer and use it in GitHub Desktop.
How to append a slash with Bottle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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