Created
June 19, 2012 10:10
-
-
Save alOneh/2953365 to your computer and use it in GitHub Desktop.
RegexConverter for Flask app
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
# source http://stackoverflow.com/questions/5870188/does-flask-support-regular-expressions-in-its-url-routing | |
from flask import Flask | |
from werkzeug.routing import BaseConverter | |
app = Flask(__name__) | |
class RegexConverter(BaseConverter): | |
def __init__(self, url_map, *items): | |
super(RegexConverter, self).__init__(url_map) | |
self.regex = items[0] | |
app.url_map.converters['regex'] = RegexConverter | |
@app.route('/<regex("[abcABC0-9]{4,6}"):uid>-<slug>/') | |
def example(uid, slug): | |
return "uid: %s, slug: %s" % (uid, slug) | |
if __name__ == '__main__': | |
app.run(debug=True, host='0.0.0.0', port=5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment